override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}
使用上述代码在任何ViewController中为特定的ViewController设置状态栏颜色为白色,在iOS8中对我来说是行不通的。有什么建议吗?使用UIApplication。shared应用方法,在信息中需要更改后颜色更改。Plist为整个应用程序。
// Change the colour of status bar from black to white
UIApplication.sharedApplication().statusBarStyle = .LightContent
我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?
我在这个问题上遇到了一些麻烦。我不太喜欢在视图中全局改变状态栏的颜色,然后在视图中改变它,然后像接受的答案一样消失。信不信由你,你可以通过在你想要的视图控制器上重写preferredStatusBarStyle来实现。经过很长一段时间,这是我所做的让它工作:
Change View controller-based status bar appearance in your info.plist to YES.
Now any full screen view controller can change the status bar style by overriding preferredStatusBarStyle.
I specify full screen because this will not work for (non-full screen) modal view controllers, not without setting modalPresentationCapturesStatusBarAppearance to Yes that is.
Also if you have embedded view controllers, like in a navigation controller for example, it will ask the top most view controller for status bar style. Overriding childViewControllerForStatusBarStyle and passing the embedded view controller is supposed to work but it didn't for me. So I just returned the embedded view controllers preferred status bar as the preferred status bar style. Something like this:
override var preferredStatusBarStyle: UIStatusBarStyle {
if let topViewController = viewControllers.last {
return topViewController.preferredStatusBarStyle
}
return .default
}
在我的情况下,我使用故事板来组织我的视图控制器。我想改变所有的状态栏风格。
你可以在下面的图片中看到。
Stars View Controller是一个CPBaseNavigationController,而CPBaseNavigationController是UINavigationController的子类。
我试着做下面的步骤:
在AppDelegate.swift func didFinishLaunchingWithOptions中添加
//更改状态栏颜色
UIApplication.sharedApplication()。statusBarHidden = false
UIApplication.sharedApplication()。statusBarStyle = .LightContent
但没有效果。
在StoryBoard中,找到Base Tab BarController(上图)。选择属性检查器,将状态条属性更改为轻内容。太糟糕了,没有效果。
最后我明白了。在我的自定义导航控制器CPBaseNavigationController中添加func preferredStatusBarStyle
override func preferredStatusBarStyle() -> UIStatusBarStyle {
返回.LightContent
}
它工作得很好!
此外,statusBarStyle在9.0中已弃用,你可以使用-[UIViewController preferredStatusBarStyle]。