How to use addSubview in iOS

Issue #344 addSubview can trigger viewDidLayoutSubviews, so be careful to just do layout stuff in viewDidLayoutSubviews This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview. Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview. When the bounds change for a view controller’€™s view, the view adjusts the positions of its subviews and then the system calls this method....

August 1, 2019 · 1 min · 146 words · Khoa

How to run app on beta iOS devices

Issue #343 Xcode 10.3 with iOS 13 sudo ln -s /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/13.0 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport Xcode 10.3 with iOS 13.1 beta 2 sudo ln -s /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/13.0/ /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/13.1 Use DeviceSupport Download and unzip Device support https://github.com/iGhibli/iOS-DeviceSupport /Applications/Xcode/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport

August 1, 2019 · 1 min · 33 words · Khoa

How to cache URLSession response

Issue #339 For simple cases, we don’t need to. Let’s use urlCache The URL cache for providing cached responses to requests within the session. Accessing Cached Data The URL Loading System caches responses both in memory and on disk, improving performance and reducing network traffic. The URLCache class is used for caching responses from network resources. Your app can directly access the shared cache instance by using the shared property of URLCache....

July 3, 2019 · 1 min · 92 words · Khoa

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 · 272 words · Khoa

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’t implement NSSecureCoding. The unarchived class doesn’t match cls, nor do any of its superclasses....

June 29, 2019 · 2 min · 312 words · Khoa

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 · 252 words · Khoa

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’s 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’s 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 · 747 words · Khoa

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 · 305 words · Khoa

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 · 92 words · Khoa

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 · 335 words · Khoa

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 · 20 words · Khoa

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 · 103 words · Khoa

Understanding device and OS share for iOS and Android

Issue #316 Browsers and devices used in 2018 AppStore measurement

June 18, 2019 · 1 min · 10 words · Khoa

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 · 24 words · Khoa

How to use new APIs in iOS

Issue #313 iOS 10 ➕UserNotifications Push user-facing notifications to the user’s device from a server, or generate them locally from your app. ➕UIViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. ➕NSPersistentContainer A container that encapsulates the Core Data stack in your app. ➕UIFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 ➕SKStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 · 2 min · 241 words · Khoa

Links for WWDC

Issue #313 iOS 10 ➕UserNotifications Push user-facing notifications to the user’s device from a server, or generate them locally from your app. ➕UIViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. ➕NSPersistentContainer A container that encapsulates the Core Data stack in your app. ➕UIFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 ➕SKStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 · 2 min · 241 words · Khoa

What is new in iOS

Issue #313 iOS 10 ➕UserNotifications Push user-facing notifications to the user’s device from a server, or generate them locally from your app. ➕UIViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. ➕NSPersistentContainer A container that encapsulates the Core Data stack in your app. ➕UIFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 ➕SKStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 · 2 min · 239 words · Khoa

What's new in iOS

Issue #313 iOS 10 ➕UserNotifications Push user-facing notifications to the user’s device from a server, or generate them locally from your app. ➕UIViewPropertyAnimator A class that animates changes to views and allows the dynamic modification of those animations. ➕NSPersistentContainer A container that encapsulates the Core Data stack in your app. ➕UIFeedbackGenerator The abstract superclass for all feedback generators. iOS 10.3 ➕SKStoreReviewController An object that controls the process of requesting App Store ratings and reviews from users....

June 17, 2019 · 2 min · 237 words · Khoa

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 · 40 words · Khoa

How to downsample image in iOS

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

June 14, 2019 · 1 min · 4 words · Khoa