Mob*_*tar 3 ios swift ios-darkmode
我正在使用 Swift 5.1 和 Xcode 11.1,目前我已经完成了暗模式设计。
用户使用此代码更改设置页面中的主题样式后,主题会立即更新。
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
appDelegate.changeTheme(themeVal)
// App Delegate File
...
func changeTheme(themeVal: String) {
if #available(iOS 13.0, *) {
switch AppState.appThemeStyle {
case "dark":
window?.overrideUserInterfaceStyle = .dark
break
case "light":
window?.overrideUserInterfaceStyle = .light
break
default:
window?.overrideUserInterfaceStyle = .unspecified
}
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是我看不到状态栏文本,因为状态栏文本颜色和视图颜色相同。
有人可以建议我好的解决方案吗?谢谢。
状态栏颜色不是全局的(默认情况下),如果您将其设置为 not ViewControllerBased,则无法再更改它。因此,您需要在您需要的任何视图中更改设置,如下所示:
var statusBarStyle = UIStatusBarStyle.default { didSet { setNeedsStatusBarAppearanceUpdate() } }
override var preferredStatusBarStyle: UIStatusBarStyle { statusBarStyle }
Run Code Online (Sandbox Code Playgroud)
这两个变量可以帮助您更改状态栏。请注意,您可以setNeedsStatusBarAppearanceUpdate在动画块内部调用以使其可动画化。
要检测何时UserInterfaceStyle更改(并相应地更新 statusBar 颜色),所有视图和 viewController 都具有委托功能。所以知道:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
updateStatusBarColor()
}
}
Run Code Online (Sandbox Code Playgroud)
这是功能:
func updateStatusBarColor() {
switch traitCollection.userInterfaceStyle {
case .unspecified: statusBarStyle = .default
case .light: statusBarStyle = .darkContent
case .dark: statusBarStyle = .lightContent
}
}
Run Code Online (Sandbox Code Playgroud)
ParentViewController 定义了statusBarColor. 因此,如果您使用的是 generalnavigationController或tabBarController,使用这些代码为他们定制的类就足够了。
| 归档时间: |
|
| 查看次数: |
4340 次 |
| 最近记录: |