我的应用程序中有多个UITabBar
,某些ViewController
具有白色状态栏,而某些ViewController
具有黑色状态栏。
我的info.plist
:
View controller-based status bar appearance
设置为YES
我的Viewcontroller中有以下代码:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .default //or return . lightContent
}
但是preferredStatusBarStyle
从未被调用。
我还在我的控制器viewDidLoad
中写了下面这行代码,但上述代码仍未被调用。
self.setNeedsStatusBarAppearanceUpdate()
我也已经多次更改了控制器的状态栏外观
为YES
和NO
,以进行测试,但仍然没有帮助。
我也尝试过以下解决方案和其他stackoverflow答案,但没有任何帮助。
iOS 13上preferredStatusBarStyle不起作用
iOS 12上preferredStatusBarStyle var不起作用?
编辑
我已经尝试了下面的代码,它会返回我topViewController
并调用该ViewController
的preferredStatusBarStyle
方法。
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return topViewController
}
}
一旦找到topViewController
,它将调用该特定ViewController
的preferredStatusBarStyle
。
但问题在于,在UITabBarController
-> UINavigationController
-> UIViewController
中它没有被调用。
需求
我有两个不同的TabBarController
。
第一个TabBarController
的状态栏样式为.lightContent
。
第二个TabBarController
的状态栏样式为.lightContent
,并且在不同的控制器中为.default
。
当我切换到第二个TabBarController
时,它将调用第二个TabBarController
的preferredStatusBarStyle
,而所有ViewController
的状态栏样式都变为.default
,但是我的某些控制器的状态栏样式希望为.lightContent
我该如何实现这个?
任何帮助将不胜感激。
谢谢
TabBarController
-->NavigationController
-->ViewController
。 - KuldeepViewController
的viewWillAppear
方法中,这样状态栏样式会正确显示1秒钟,然后自动切换到.light
。 - Kuldeepcontroller-based status bar appearance
,并在viewWillAppear
中调用以下代码:UIApplication.shared.statusBarStyle = .default setNeedsStatusBarAppearanceUpdate()
。 - Alexandr Kolesnik