英文:
preferredStatusBarStyle not getting called in iOS 13 and other
问题
我在我的应用程序中有多个 UITabBar
,一些 ViewController
的状态栏是白色的,而一些 ViewController
的状态栏是黑色的。
我的 info.plist
将 View controller-based status bar appearance
设置为 YES
我的 Viewcontroller 有以下代码。
override var preferredStatusBarStyle: UIStatusBarStyle {
return .default // 或者 return .lightContent
}
但是 preferredStatusBarStyle
从未被调用。
我还在我的控制器的 viewDidLoad
中写了下面的代码,但仍然没有被调用。
self.setNeedsStatusBarAppearanceUpdate()
我还尝试了多次更改 controller-based status bar appearance
为 YES
和 NO
,但没有任何帮助。
我还尝试了以下解决方案和其他 Stack Overflow 答案,但没有帮助。
编辑
我尝试了下面的代码,它返回了 topViewController
,并调用了该 ViewController
的 preferredStatusBarStyle
。
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return topViewController
}
}
一旦找到了 topViewController
,它将调用该特定 ViewController
的 preferredStatusBarStyle
。
但问题是,在 UITabBarController
-> UINavigationController
-> UIViewController
中它不会被调用。
要求
我有2个不同的 TabBarController
。
第一个 TabBarController
的状态栏样式是 .lightContent
。
第二个 TabBarController
的状态栏样式是 .lightContent
,并且在不同的控制器中是 .default
。
当我切换到第二个 TabBarController
时,它将调用第二个 TabBarController
的 preferredStatusBarStyle
,并且所有 ViewController
的状态栏样式都变为 .default
,但我的一些控制器的状态栏样式需要是 .lightContent
。
如何实现这个目标?
任何帮助将不胜感激。
谢谢!
英文:
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.
https://stackoverflow.com/questions/58203902/preferredstatusbarstyle-not-respecting-on-ios-13
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
答案1
得分: 3
这与iOS 13无关。你只是弄错了规则。
在导航控制器情况下,状态栏的颜色不是由视图控制器的preferredStatusBarStyle确定的。
令人惊讶的是,它是由导航栏的barStyle确定的。要获取浅色状态栏文本,请在你的视图控制器中执行以下操作:
self.navigationController?.navigationBar.barStyle = .black
英文:
This has nothing to do with iOS 13. You just have the rules wrong.
In a navigation controller situation, the color of the status bar is not determined by the view controller’s preferredStatusBarStyle.
It is determined, amazingly, by the navigation bar’s barStyle. To get light status bar text, say (in your view controller):
self.navigationController?.navigationBar.barStyle = .black
答案2
得分: 2
Here is the translated version of the code you provided:
我已经找到解决方案。
将以下代码放置以查找“topViewController”。
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return topViewController
}
}
一旦它找到“topViewController”,则以下代码将在您当前的“ViewController”中被调用,并且您可以根据需要设置statusBarStyle
。
override var preferredStatusBarStyle: UIStatusBarStyle { }
在我的情况下,我有两个“TabBar”。
第一个“TabBar”控制器的状态栏样式是.lightContent
,第二个“TabBar”控制器的状态栏样式是.default
,因此创建两个UITabBarController
。第一个用于.lightContent
,第二个用于.default
,并在其中放置preferredStatusBarStyle
。
因此,当您位于UITabBarController
的子控制器时,您的UITabBarController
的preferredStatusBarStyle
将被调用,并且子控制器的statusBarStyle
将根据您设置的样式进行设置。
英文:
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.
答案3
得分: 1
请参考以下链接:
override var modalPresentationCapturesStatusBarAppearance: Bool {
set {}
get{
return true
}
}
英文:
please refer to this
override var modalPresentationCapturesStatusBarAppearance: Bool {
set {}
get{
return true
}
}
答案4
得分: -2
以下内容可能对您有所帮助:
对于开发人员指南,请参阅 UIStatusBarStyle 常量在 UIApplicenter code hereation 中以及 UIViewController 中的 preferredStatusBarStyle 属性。
英文:
the following may be of assistance to you;
> For developer guidance, see the UIStatusBarStyle constant in
> UIApplicenter code hereation and the preferredStatusBarStyle
> property in UIViewController.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论