在iOS 13和其他系统中未调用preferredStatusBarStyle [英] preferredStatusBarStyle not getting called in iOS 13 and other
问题描述
我的应用程序中有多个 UITabBar
,有些 ViewController
具有白色状态栏,有些 ViewController
具有黑色状态栏.
我的 info.plist
查看基于控制器状态栏的状态
到 YES
我的Viewcontroller具有以下代码.
override var preferredStatusBarStyle:UIStatusBarStyle {return .default//或returnlightContent}
但 preferredStatusBarStyle
从未被调用.
我还在控制器 viewDidLoad
中的下面写了一行,但仍然没有被调用.
self.setNeedsStatusBarAppearanceUpdate()
我也将基于控制器的状态栏外观更改为<是>是&&否
可以多次检查,但对我没有帮助.
我还尝试了以下解决方案和其他stackoverflow答案,但没有任何帮助.
preferredStatusBarStyle在iOS 13上不适用
preferredStatusBarStyle var在iOS12中不起作用? >
编辑
我尝试了下面的代码,该代码返回了 topViewController
,它将调用该 ViewController
preferredStatusBarStyle
扩展UINavigationController {重写open var childForStatusBarStyle:UIViewController吗?{返回topViewController}}
因此,一旦找到 topViewController
,它将调用该特定 ViewController
的 preferredStatusBarStyle
.
但是问题是它没有在 UITabBarController
-> UINavigationController
-> UIViewController
内部被调用.
要求
我有2个不同的 TabBarController
.
第一个 TabBarController
statusBarStyle是 .lightContent
.
第二个 TabBarController
statusBarStyle在不同的控制器中是 .lightContent
和 .default
.
当我切换到第二个TabBarController时,它将调用第二个 TabBarController
的 preferredStatusBarStyle
,并且所有 ViewController
statusBarStyle都变为 .default 代码>,但是我的某些控制器statusBarStyle希望为
.ligthContent
我该如何实现?
任何帮助将不胜感激.
谢谢
我找到了解决方案.
在下面的代码中找到 topViewController
.
扩展UINavigationController {重写open var childForStatusBarStyle:UIViewController吗?{返回topViewController}}
因此一旦找到 topViewController
,下面的代码就会在当前的 ViewController
中被调用,您可以根据需要设置 statusBarStyle
.
覆盖var preferredStatusBarStyle:UIStatusBarStyle {}
在我的情况下,我有2个 TabBar
.
第一个 TabBar
控制器为 .lightContent
,第二个 TabBar
控制器为 .default
,因此创建2个 UITabBarController
.第一个用于 .lightContent
,第二个用于 .default
,并将 preferredStatusBarStyle
放入其中.
因此,当您位于 UITabBarController
子控制器时,您的 UITabBarController
preferredStatusBarStyle
会被调用,子控制器 statusBarStyle
会被设置按照您设定的样式.
I have multiple UITabBar
in my application and some ViewController
has White color statusbar and some ViewController
has black color statusbar.
My info.plist
View controller-based status bar appearance
to YES
My Viewcontroller has below code.
override var preferredStatusBarStyle: UIStatusBarStyle {
return .default //or return . lightContent
}
but preferredStatusBarStyle
never getting called.
i have also written below line in my controller viewDidLoad
but still above wasn't getting called.
self.setNeedsStatusBarAppearanceUpdate()
also i have changeed controller-based status bar appearance
to YES
&& NO
for multiple time to check but nothing helps to me.
I have also tried below solutions and other stackoverflow answers but nothing helps me.
preferredStatusBarStyle not respecting on iOS 13
preferredStatusBarStyle var not working in iOS12?
EDIT
I have tried below code which returns me the topViewController
and it will call the preferredStatusBarStyle
of that ViewController
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return topViewController
}
}
so once the topViewController
found it will call preferredStatusBarStyle
of that particular ViewController
.
but the issue is that it wasn't getting called inside UITabBarController
-> UINavigationController
-> UIViewController
.
Requirment
I have 2 different TabBarController
.
1st TabBarController
statusBarStyle is .lightContent
.
2nd TabBarController
statusBarStyle is .lightContent
and .default
in different controller.
When i change to the 2nd TabBarController it will call preferredStatusBarStyle
of 2nd TabBarController
and all ViewController
statusBarStyle goes .default
but some of my controller statusBarStyle wants to be of .ligthContent
How can i achieve this?
any help will be appreciated.
Thanks
i got the solution.
Put below code to find the topViewController
.
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return topViewController
}
}
so once it finds topViewController
, below code getting called in your current ViewController
and you can set statusBarStyle
as per requirement.
override var preferredStatusBarStyle: UIStatusBarStyle { }
In my case i have 2 TabBar
.
1st TabBar
controllers are of .lightContent
and 2nd TabBar
controllers are of .default
so create 2 UITabBarController
. 1st is for .lightContent
and 2nd is for .default
and put preferredStatusBarStyle
inside it.
so when you are at UITabBarController
sub controller, your UITabBarController
preferredStatusBarStyle
getting called and sub controller statusBarStyle
set as per your set style.
这篇关于在iOS 13和其他系统中未调用preferredStatusBarStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!