Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created August 18, 2019 14:16
Show Gist options
  • Save prafullakumar/f6420942011ee5f0dc37cda5b3fd4c0c to your computer and use it in GitHub Desktop.
Save prafullakumar/f6420942011ee5f0dc37cda5b3fd4c0c to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
var userInterfaceStyle: UIUserInterfaceStyle?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
userInterfaceStyle = self.traitCollection.userInterfaceStyle
setupUI()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
//update user interface
setupUI()
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
// Trait collection will change. Use this one so you know what the state is changing to.
userInterfaceStyle = newCollection.userInterfaceStyle
}
private func setupUI() {
switch userInterfaceStyle {
case .dark:
// User Interface is Dark
()
case .light:
// User Interface is Light
()
case .unspecified:
//your choice
()
case .none:
// Optional default case new introduction
()
@unknown default:
()
//Switch covers known cases, but 'UIUserInterfaceStyle' may have additional unknown values, possibly added in future versions
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment