Skip to content

Commit

Permalink
Remove the traitCollection fallback on iOS 13
Browse files Browse the repository at this point in the history
On iOS 13 UIKit now predicts the initial traits for a view before it's rendered so no need to return the screen's trait collection instead. Moreover, it's better to use the app's key window for determing the fallback traitCollection instead of the screen since for apps that support split screen that will be different based on the configuration chosen by the user.
  • Loading branch information
nataliq-pp committed Jul 24, 2019
1 parent 51ba63b commit a27685d
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions Flow/UIView+Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ public extension UIView {
}

public extension UITraitEnvironment {
/// Returns the current traitCollection or the screen's traitCollection if `self` has no window
/// Returns the current traitCollection.
///
/// Prior iOS 13 (where the traitCollection is always available), there is a fallback if `self` has no window - it falls back to the app's key window traitCollection or if that's not available to the main screen's traitCollection.
var traitCollectionWithFallback: UITraitCollection {
return hasWindowTraitCollection ?? UIScreen.main.traitCollection
guard #available (iOS 13, *) else {
switch self {
case let view as UIView where view.window != nil: return view.traitCollection
case let viewController as UIViewController where viewController.isViewLoaded && viewController.view?.window != nil: return viewController.traitCollection
default: return UIApplication.shared.keyWindow?.traitCollection ?? UIScreen.main.traitCollection
}
}
return self.traitCollection
}
}

Expand Down Expand Up @@ -86,16 +95,6 @@ public extension UIView {
}
}

private extension UITraitEnvironment {
var hasWindowTraitCollection: UITraitCollection? {
switch self {
case let view as UIView where view.window != nil: return view.traitCollection
case let viewController as UIViewController where viewController.isViewLoaded && viewController.view?.window != nil: return viewController.traitCollection
default: return nil
}
}
}

private extension UIView {
func signal<T>(for keyPath: KeyPath<CallbackerView, Callbacker<T>>) -> Signal<T> {
return Signal { callback in
Expand Down

0 comments on commit a27685d

Please sign in to comment.