我正在带有动画的侧边菜单 View Controller ( ENSwiftSideMenu ) 中移动。由于侧面菜单的背景颜色是深蓝色,我希望状态栏(默认情况下为黑色)在菜单可见时变亮。
在 info.plist
中将 View controller-based status bar appearance
设置为 YES
(也尝试过 NO
) ,我尝试实现以下代码,不幸的是,它不起作用:
UIApplication.shared.statusBarStyle = .lightContent
另外,我试过这个:
override func viewDidLoad() {
super.viewDidLoad()
setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .default
}
知道如何让它发挥作用吗?
谢谢!
最佳答案
试试这段代码:
在你的 AppDelegate 中:
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] // If you want to change title colour
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
//用下面的代码更新你的plist
View controller-based status bar appearance = NO
在你的 ViewController 中:
override func viewDidLoad() {
super.viewDidLoad()
// UIApplication.sharedApplication().statusBarStyle = .LightContent
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController?.navigationBar.translucent = true
}
func sideMenuWillOpen() {
print("sideMenuWillOpen")
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
func sideMenuWillClose() {
print("sideMenuWillClose")
UIApplication.sharedApplication().statusBarStyle = .Default
}
上述代码的输出。您可以使用某种 UIAnimation 来同步效果。
让我知道。如果代码适合您...
关于ios - 实时更改状态栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40059328/