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
}
Swift 4.2解决方案与NavigationController
第一步:
打开你的信息。并插入一个名为“View controller based status bar appearance”或UIViewControllerBasedStatusBarAppearance的新键到YES,让每个VC使用自己的状态属性。
第二步
在每个VC中,像这样重写preferredStatusBarStyle属性:
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent //.default for black style
}
最后一步
重写自定义NavigationController类中的preferredStatusBarStyle属性:
class NavigationController : UINavigationController {
override var preferredStatusBarStyle : UIStatusBarStyle {
if let topVC = viewControllers.last {
//return the status property of each VC, look at step 2
return topVC.preferredStatusBarStyle
}
return .default
}
警告
'statusBarStyle'的Setter在iOS 9.0中已弃用:
UIApplication.shared.statusBarStyle = .default
所以我的解决方案是这样的:
从导航控制器做一个扩展:
extension UINavigationController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
if let topViewController = presentedViewController{
return topViewController.preferredStatusBarStyle
}
if let topViewController = viewControllers.last {
return topViewController.preferredStatusBarStyle
}
return .default
}
}
如果你有一个viewController它会有另一个样式而不是app的样式,你可以做这个
var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
return barStyle
}
假设你的app状态样式是。default,你希望这个屏幕是。lightcontent
所以barStyle会把。lightContent作为它的默认值,这将改变状态栏的样式为lightContent,然后确保当viewWillDisappear再次改变barStyle为应用程序状态栏的样式,在我们的例子中是。default。
这对我很有用
我可以给你一个更简单的方法,
就像苹果文档说的那样,在viewDidLoad中调用setNeedsStatusBarAppearanceUpdate,
如果视图控制器的状态栏属性(如隐藏/不隐藏状态或样式)发生变化,则调用此方法。如果在动画块中调用此方法,则更改将与动画块的其余部分一起动画化。
实现preferredStatusBarStyle返回您的首选类型。
我在iOS 10.1系统中使用了这个功能。
Objective - C
[self setNeedsStatusBarAppearanceUpdate];
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
斯威夫特
setNeedsStatusBarAppearanceUpdate()
var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
我很惊讶没有人指出这一点。无论如何,喜欢:)