How to do custom presentation with UIViewPropertyAnimator in iOS

Issue #337 Normally we just present from any UIViewController in any UINavigationController in UITabBarController and it will present over tabbar present(detailViewController, animated: true, completion: nil) If we have animation with UIViewPropertyAnimator, then we can implement UIViewControllerAnimatedTransitioning and interruptibleAnimator(using:) The methods in this protocol let you define an animator object, which creates the animations for transitioning a view controller on or off screen in a fixed amount of time. The animations you create using this protocol must not be interactive....

July 2, 2019 路 2 min 路 Khoa Pham

How to use NSSecureCoding in Swift

Issue #334 NSSecureCoding has been around since iOS 6 and has had some API changes in iOS 12 A protocol that enables encoding and decoding in a manner that is robust against object substitution attacks. https://developer.apple.com/documentation/foundation/nscoder/2292924-decodeobject If the coder responds true to requiresSecureCoding, then the coder calls failWithError(_:) in either of the following cases: The class indicated by cls doesn鈥檛 implement NSSecureCoding. The unarchived class doesn鈥檛 match cls, nor do any of its superclasses....

June 29, 2019 路 2 min 路 Khoa Pham

How to simplify pager interaction with Rx

Issue #333 In a traditional pager with many pages of content, and a bottom navigation with previous and next button. Each page may have different content, and depending on each state, may block the next button. The state of next button should state in real time depending on state in each page content, and when user moves back and forth between pages, the state of next button should be reflected as well....

June 27, 2019 路 2 min 路 Khoa Pham

How to handle keyboard for UITextField in scrolling UIStackView in iOS

Issue #329 Firstly, to make UIStackView scrollable, embed it inside UIScrollView. Read How to embed UIStackView inside UIScrollView in iOS It鈥檚 best to listen to keyboardWillChangeFrameNotification as it contains frame changes for Keyboard in different situation like custom keyboard, languages. Posted immediately prior to a change in the keyboard鈥檚 frame. class KeyboardHandler { let scrollView: UIScrollView let stackView: UIStackView var observer: AnyObject? var keyboardHeightConstraint: NSLayoutConstraint! struct Info { let frame: CGRect let duration: Double let animationOptions: UIView....

June 26, 2019 路 4 min 路 Khoa Pham

How to make simple form validator in Swift

Issue #328 Sometimes we want to validate forms with many fields, for example name, phone, email, and with different rules. If validation fails, we show error message. We can make simple Validator and Rule class Validator { func validate(text: String, with rules: [Rule]) -> String? { return rules.compactMap({ $0.check(text) }).first } func validate(input: InputView, with rules: [Rule]) { guard let message = validate(text: input.textField.text ?? "", with: rules) else { input....

June 25, 2019 路 2 min 路 Khoa Pham

How to deal with weak in closure in Swift

Issue #326 Traditionally, from Swift 4.2 we need guard let self addButton.didTouch = { [weak self] in guard let self = self, let product = self.purchasedProduct() else { return self.delegate?.productViewController(self, didAdd: product) } This is cumbersome, we can invent a higher order function to zip and unwrap the optionals func with<A, B>(_ op1: A?, _ op2: B?, _ closure: (A, B) -> Void) { if let value1 = op1, let value2 = op2 { closure(value1, value2) } } addButton....

June 25, 2019 路 1 min 路 Khoa Pham

How to make material UITextField with floating label in iOS

Issue #325 Use UILabel as placeholder and move it When label is moved up, scale it down 80%. It means it has 10% padding on the left and right when shrinked, so offsetX for translation is 10% Translation transform should happen before scale Ideally we can animate font and color change using CATextLayer, but with UILabel we can use UIView.transition final class MaterialInputView: UIView { lazy var label: UILabel = { return UILabel() }() lazy var textField: UITextField = { let textField = UITextField() textField....

June 24, 2019 路 2 min 路 Khoa Pham

How to embed UIStackView inside UIScrollView in iOS

Issue #324 view.addSubview(scrollView) scrollView.addSubview(stackView) NSLayoutConstraint.on([ scrollView.pinEdges(view: view), stackView.pinEdges(view: scrollView) ]) NSLayoutConstraint.on([ stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, multiplier: 1.0) ]) Updated at 2020-09-07 14:28:15

June 24, 2019 路 1 min 路 Khoa Pham

How to make custom controller for View in iOS

Issue #318 I do UI in code, and usually separate between View and ViewController. class ProfileView: UIView {} class ProfileViewController: UIViewController { override func loadView() { self.view = ProfileView() } } But in places where using UIViewController and manage their view controller containment hierarchy is not desired, then we can roll out a normal object to act as the controller. class ProfileController { let profileView: ProfileView init(profileView: ProfileView) { self.profileView = profileView } func update(profile: Profile) { profileView....

June 19, 2019 路 1 min 路 Khoa Pham

Understanding device and OS share for iOS and Android

Issue #316 Browsers and devices used in 2018 AppStore measurement

June 18, 2019 路 1 min 路 Khoa Pham

How to make UIPanGestureRecognizer work with horizontal swipe in UICollectionView

Issue #315 extension PanCollectionViewController: UIGestureRecognizerDelegate { func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { let velocity = panGR.velocity(in: panGR.view) return abs(velocity.y) > abs(velocity.x) } }

June 18, 2019 路 1 min 路 Khoa Pham

How to use new APIs in iOS

Issue #313 iOS 10 鉃昒serNotifications Push user-facing notifications to the user鈥檚 device from a server, or generate them locally from your app. 鉃昒IViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. 鉃昇SPersistentContainer A container that encapsulates the Core Data stack in your app. 鉃昒IFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 鉃昐KStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 路 2 min 路 Khoa Pham

Links for WWDC

Issue #313 iOS 10 鉃昒serNotifications Push user-facing notifications to the user鈥檚 device from a server, or generate them locally from your app. 鉃昒IViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. 鉃昇SPersistentContainer A container that encapsulates the Core Data stack in your app. 鉃昒IFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 鉃昐KStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 路 2 min 路 Khoa Pham

What is new in iOS

Issue #313 iOS 10 鉃昒serNotifications Push user-facing notifications to the user鈥檚 device from a server, or generate them locally from your app. 鉃昒IViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. 鉃昇SPersistentContainer A container that encapsulates the Core Data stack in your app. 鉃昒IFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 鉃昐KStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 路 2 min 路 Khoa Pham

What's new in iOS

Issue #313 iOS 10 鉃昒serNotifications Push user-facing notifications to the user鈥檚 device from a server, or generate them locally from your app. 鉃昒IViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. 鉃昇SPersistentContainer A container that encapsulates the Core Data stack in your app. 鉃昒IFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 鉃昐KStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 路 2 min 路 Khoa Pham

How to get cell at center during scroll in UICollectionView

Issue #311 See Omnia https://github.com/onmyway133/Omnia/blob/master/Sources/iOS/UICollectionView.swift#L30 extension HorizontalUsersViewController: UIScrollViewDelegate { func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { let point = view.convert(collectionView.center, to: collectionView) guard let indexPath = collectionView.indexPathForItem(at: point), indexPath.item < users.count else { return } let user = users[indexPath.item] delegate?.didScrollTo(user) } }

June 14, 2019 路 1 min 路 Khoa Pham

How to downsample image in iOS

Issue #310 https://www.swiftjectivec.com/optimizing-images/ https://nshipster.com/image-resizing/

June 14, 2019 路 1 min 路 Khoa Pham

How to show location in Apple Maps and Google Maps app in iOS

Issue #309 Apple Maps let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil) let mapItem = MKMapItem(placemark: placemark) mapItem.name = shop.name mapItem.openInMaps(launchOptions: [:]) Google Maps https://developers.google.com/maps/documentation/urls/ios-urlscheme Use q to show pin Since iOS 9, we need to declare LSApplicationQueriesSchemes <key>LSApplicationQueriesSchemes</key> <array> <string>comgooglemaps</string> </array> var string = "comgooglemaps://" string += "?q=Food" string += "¢er=\(coordinate.latitude),\(coordinate.longitude)" string += "&zoom=15" let googleUrl = URL(string: string)! if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!) { UIApplication.shared.open(googleUrl) }

June 13, 2019 路 1 min 路 Khoa Pham

How to format distance in iOS

Issue #307 import MapKit let formatter = MKDistanceFormatter() formatter.unitStyle = .abbreviated formatter.units = .metric distanceLabel.text = formatter.string(fromDistance: distance) // 700m, 1.7km

June 13, 2019 路 1 min 路 Khoa Pham

How to test drive view in iOS

Issue #303 Instead of setting up custom framework and Playground, we can just display that specific view as root view controller window.rootViewController = makeTestPlayground() func makeTestPlayground() -> UIViewController { let content = UserCell() content.nameLabel.text = "Thor" content.streetLabel.text = "Asgard" content.weaponLabel.text = "Hammer" let viewController = UIViewController() viewController.view.backgroundColor = .white viewController.view.addSubview(content) NSLayoutConstraint.on([ content.centerXAnchor.constraint(equalTo: viewController.view.centerXAnchor), content.centerYAnchor.constraint(equalTo: viewController.view.centerYAnchor), content.widthAnchor.constraint(equalTo: viewController.view.widthAnchor, multiplier0.9), content.heightAnchor.constraint(equalToConstant: 200) ] return viewController }

June 12, 2019 路 1 min 路 Khoa Pham