在XCode 7.3.x中,ill用以下方式更改了我的StatusBar的背景色:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但这似乎不再适用于SWIFT3.0。
我试过:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但它给了我:
this class is not key value coding-compliant for the key statusBar.
有什么想法吗?如何用XCode8 8/SWIFT3.0来改变它?
发布于 2016-09-30 18:57:12
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
iOS 13更新
名为-statusBar或-statusBarWindow on UIApplication的应用程序:这段代码必须更改,因为不再有状态栏或状态栏窗口。在窗口场景中使用statusBarManager对象。
发布于 2017-04-06 11:51:04
“更改”状态栏背景色:
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
let statusBarColor = UIColor(red: 32/255, green: 149/255, blue: 215/255, alpha: 1.0)
statusBarView.backgroundColor = statusBarColor
view.addSubview(statusBarView)
更改状态栏文本颜色:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
更新:请注意,当视图旋转时,状态栏框架将发生变化。您可以通过以下方式更新创建的子视图框架:
statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
viewWillLayoutSubviews()
发布于 2018-08-10 06:02:30
使用Swift 3和4,您可以使用下面的代码片段。它使用UIApplication
设置的背景色从valueForKeyPath
查找视图。
guard let statusBarView = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else {
return
}
statusBarView.backgroundColor = UIColor.red
目标-C
UIView *statusBarView = [UIApplication.sharedApplication valueForKeyPath:@"statusBarWindow.statusBar"];
if (statusBarView != nil)
{
statusBarView.backgroundColor = [UIColor redColor];
}
https://stackoverflow.com/questions/39802420
复制