Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
I’m including possibility so as to add possibility for consumer to decide on system/mild/darkish modes.
I’m storing consumer alternative in userdefaults. Beneath is my code in AppDelegate and buyer UINavigationController class.
Challenge is when machine is in darkish mode and consumer has chosen mild mode then standing bar isn’t seen. Equally, if machine is in mild mode however consumer has chosen darkish mode then standing bar isn’t white.
Tried options proven on some posts however not capable of get round.
AppDelegare
if let currentUserSelectedInterfaceStyle = userDefaults.string(forKey: "currentUserInterfaceStyle") {
swap currentUserSelectedInterfaceStyle {
case "mild" :
window?.overrideUserInterfaceStyle = UIUserInterfaceStyle.mild
case "darkish" :
window?.overrideUserInterfaceStyle = UIUserInterfaceStyle.darkish
case "system":
window?.overrideUserInterfaceStyle = UIUserInterfaceStyle.unspecified
default:
window?.overrideUserInterfaceStyle = UIUserInterfaceStyle.unspecified
}
} else {
userDefaults.set("mild", forKey: "currentUserInterfaceStyle")
userDefaults.synchronize()
window?.overrideUserInterfaceStyle = .mild
}
}
Customized navigation controller class
is setting statusBarStyle to UIStatusBarStyle.default a problem? However i can not set it to different worth earlier than nav controller is instantiated.
class DarkModeAwareNavigationController: UINavigationController {
var statusBarStyle = UIStatusBarStyle.default { didSet
{
setNeedsStatusBarAppearanceUpdate()
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
statusBarStyle
}
override open var childForStatusBarStyle: UIViewController? {
return self.topViewController
}
override init(rootViewController: UIViewController) {
tremendous.init(rootViewController: rootViewController)
updateStatusBarColor()
}
required init?(coder aDecoder: NSCoder) {
tremendous.init(coder: aDecoder)
updateStatusBarColor()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
tremendous.traitCollectionDidChange(previousTraitCollection)
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
updateStatusBarColor()
}
}
}
func updateStatusBarColor() {
swap UIScreen.important.traitCollection.userInterfaceStyle
{
case .unspecified:
statusBarStyle = .lightContent
case .mild:
statusBarStyle = .darkContent
case .darkish:
statusBarStyle = .lightContent
default:
statusBarStyle = .lightContent
}
}
}
[ad_2]