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
我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?
自定义状态栏颜色(iOS11+, Swift4+)
如果您正在寻找如何将状态栏更改为自定义颜色的解决方案,这是可行的解决方案。
let statusBarView = UIView()
view.addSubview(statusBarView)
statusBarView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
statusBarView.topAnchor.constraint(equalTo: view.topAnchor),
statusBarView.leftAnchor.constraint(equalTo: view.leftAnchor),
statusBarView.rightAnchor.constraint(equalTo: view.rightAnchor),
statusBarView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
statusBarView.backgroundColor = .blue
我可以给你一个更简单的方法,
就像苹果文档说的那样,在viewDidLoad中调用setNeedsStatusBarAppearanceUpdate,
如果视图控制器的状态栏属性(如隐藏/不隐藏状态或样式)发生变化,则调用此方法。如果在动画块中调用此方法,则更改将与动画块的其余部分一起动画化。
实现preferredStatusBarStyle返回您的首选类型。
我在iOS 10.1系统中使用了这个功能。
Objective - C
[self setNeedsStatusBarAppearanceUpdate];
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
斯威夫特
setNeedsStatusBarAppearanceUpdate()
var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
我很惊讶没有人指出这一点。无论如何,喜欢:)
在我的情况下,我使用故事板来组织我的视图控制器。我想改变所有的状态栏风格。
你可以在下面的图片中看到。
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]。
对于Xcode 10,你可以创建一个类并把它放在你的viewController类之前,你可以在所有视图控制器中调用这个类,需要一个轻内容状态栏…
class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
现在在下面修改你的viewController类:
class YourViewController: UIViewControllerWithLightStatusBar {
...
}
这就是全部……
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
}