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
}
我已经设置了特定的颜色(在RGB格式)使用下面的代码在应用程序委托文件:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
. . .
UIApplication.sharedApplication().statusBarHidden = false
UIApplication.sharedApplication().statusBarStyle = .LightContent
let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
if statusBar.respondsToSelector(Selector("setBackgroundColor:")) {
statusBar.backgroundColor = UIColor.init(red: 0.1, green: 0.27, blue: 0.60, alpha: 1.0)
}
. . .
}
您还需要添加以下关键信息。Plist文件:
查看基于控制器的状态栏外观,布尔值设置为NO
我可以给你一个更简单的方法,
就像苹果文档说的那样,在viewDidLoad中调用setNeedsStatusBarAppearanceUpdate,
如果视图控制器的状态栏属性(如隐藏/不隐藏状态或样式)发生变化,则调用此方法。如果在动画块中调用此方法,则更改将与动画块的其余部分一起动画化。
实现preferredStatusBarStyle返回您的首选类型。
我在iOS 10.1系统中使用了这个功能。
Objective - C
[self setNeedsStatusBarAppearanceUpdate];
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
斯威夫特
setNeedsStatusBarAppearanceUpdate()
var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
我很惊讶没有人指出这一点。无论如何,喜欢:)
对于Xcode 10,你可以创建一个类并把它放在你的viewController类之前,你可以在所有视图控制器中调用这个类,需要一个轻内容状态栏…
class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
现在在下面修改你的viewController类:
class YourViewController: UIViewControllerWithLightStatusBar {
...
}
这就是全部……
我在Swift 5、Swift 4.2中使用这种方式。
在Info.plist中添加下一个值:
UIViewControllerBasedStatusBarAppearance = YES
or
UIViewControllerBasedStatusBarAppearance = NO(查看变化)
UIStatusBarHidden = NO
UIStatusBarStyle = UIStatusBarStyleDefault(或设置为UIStatusBarStyleLightContent,如果你想在启动时看到轻状态栏文本)
然后将代码放置到您想要看到浅色内容的特定视图控制器(要看到深色文本,请将preferredStatusBarStyle设置为. darkcontent)。
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
statusBar.backgroundColor = .sunflowerYellow
}
}