在XCode 7.3.x中,我使用以下命令更改了StatusBar的背景颜色:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但似乎这不再适用于Swift 3.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 / Swift 3.0进行更改?
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector("statusBar")) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
在扩展程序文件中添加以下代码,以便在swift 4及更高版本中编辑状态栏:
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
现在,我们可以通过在ViewController类中添加以下行来编辑状态栏:
let frame = screenController.view.convert(UIApplication.shared.statusBarFrame, to: screenController.view)
let backview = UIView(frame: frame)
backview.backgroundColor = backgroundColor
screenController.view.addSubview(backview)
screenController.view.bringSubviewToFront(backview)
希望,这会有所帮助。谢谢
适用于Swift 4+和iOS 13+的解决方案
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = <Your Color name>
ex: UIApplication.shared.statusBarView?.backgroundColor = .red
“更改”状态栏背景颜色:
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()
使用Swift 3和4,您可以使用下面的代码片段。它使用UIApplication
找到来自valueForKeyPath
的视图设置它的背景颜色。
guard let statusBarView = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else {
return
}
statusBarView.backgroundColor = UIColor.red
Objective-C的
UIView *statusBarView = [UIApplication.sharedApplication valueForKeyPath:@"statusBarWindow.statusBar"];
if (statusBarView != nil)
{
statusBarView.backgroundColor = [UIColor redColor];
}
对于Xcode 9和iOS 11:我们将尝试实现的状态栏的样式是具有白色内容的状态栏。转到ViewController.swift文件并添加以下代码行。
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
或者从项目设置选项中,您可以更改状态栏的样式:
接下来,返回故事板,选择View Controller,然后在Editor菜单中选择Embed in Navigation Controller。选择导航栏,然后在“属性”检查器中将“条形”颜色设置为红色。故事板将如下所示。
构建并运行项目,状态栏的内容再次变暗,这是默认值。原因是,iOS要求导航控制器的状态栏的样式而不是包含的视图控制器。
要更改应用程序内所有导航控制器的样式,请在AppDelegate.swift文件中更改以下方法。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}
再次构建并运行项目,这次状态栏的内容变为白色。
对于iOS 11和Xcode 9,请使用以下步骤。
查看基于控制器的状态栏外观
试试这个
转到你的应用info.plist
然后转到您的应用程序委托并将以下代码粘贴到您设置Windows的RootViewController的位置。
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
这对我有用,因为我的导航栏TintColor是黑色的,无法看到状态栏。
当设置上面的代码时,它确实将FininLaunch状态栏显示为白色。
您可以在应用程序启动期间或视图控制器的viewDidLoad期间为状态栏设置背景颜色。
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor blackColor];
[self.window.rootViewController.view addSubview:view];
}
结果如下:
可能的解决方案是在viewController上添加将用作statusBar背景的视图:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any] ?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}