iOS 自定义状态栏背景颜色未显示
iOS Custom Status Bar Background Color not displaying
我正在尝试使用以下方法将状态栏背景颜色填充为橙色
UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
但是,我得到了一个白色的状态栏,应该用橙色填充而不是按照这个例子:Customize navigation bar appearance with swift
我在 AppDelegate.swift 文件中 didFinishLaunchingWithOptions 方法下进行设置,以将其应用于整个应用程序。
我已将 info.plist 编辑为以下内容:View controller-based status bar appearance => NO
有谁知道我做错了什么吗?
编辑:我不确定它是否重要,但视图在 UITabBarController
中
编辑 2:这实际上发生在所有视图中,而不仅仅是 UITabBarController。
编辑 3:谢谢 @Utsav Parikh
我现在在状态栏的顶部添加一个视图,当应用程序加载状态栏时,它会短暂显示橙色,但是一旦完成加载,它就会被推离视图并替换为通用的白色状态栏。
为什么会这样?
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window!.rootViewController!.view.addSubview(view)
编辑 Swift 3:
与UITabBarController
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
没有嵌入式控制器
我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式控制器的情况下学会了一些技巧:
在您的 AppDelegate.swift 中添加此方法并在 didFinishLaunchingWithOptions
中调用它
func customizeAppearance() {
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UITabBar.appearance().barTintColor = UIColor.black
let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
UITabBar.appearance().tintColor = tintColor
}
在 AppDelegate
的 didFinishLaunchingWithOptions 中添加这段代码
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window.rootViewController.view.addSubview(view)
希望对您有所帮助....!!!
我认为你的最后一行正在恢复你的更改,试试这个:
override func viewWillAppear(animated: Bool) {
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
super.viewWillAppear(animated)
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.orangeColor()
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
}
tintColor
和更改 UINavigationBar
的背景颜色有一个主要区别。我认为最好的方法是应用背景图像,由只有一种颜色的 1 像素正方形图像制成。
像那样:
let tabbarAndNavBarBkg = UIImage(named: "nav_tab")
UINavigationBar.appearance().setBackgroundImage(tabbarAndNavBarBkg, forBarMetrics: .Default)
或者您可以在 UIColor
上创建一个类别 return 一个 UIImage
给定一个 UIColor
实例,在 objC:
+ (UIImage *) imageWithColor:(UIColor*) color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return colorImage;
}
UINavigationBar.appereance()
适用于即将到来的 viewController,但不适用于当前显示的 rootViewController。为此,我在 didFinishLaunchingWithOptions
中添加了以下内容:
UINavigationBar.appearance().tintColor = myColor
let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
navigationController.navigationBar.barTintColor = myColor
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : myTextColor]
navigationController.navigationBar.translucent = false
navigationController.setNeedsStatusBarAppearanceUpdate()
在你在 info.plist
中所做的之后:View controller-based status bar appearance
=> NO.
在 didFinishLaunchingWithOptions
下的 AppDelegate.swift
文件中添加此代码:
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x2E9AFE)
// change navigation item title color
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
并且您可以 select 任何十六进制代码来选择颜色..!!享受..!!
抱歉,忘记使用十六进制码了,您也需要这个,所以请将此代码添加到 AppDelegate.swift
:
中的任意位置
func uicolorFromHex(rgbValue:UInt32)->UIColor {
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
let blue = CGFloat(rgbValue & 0xFF)/256.0
return UIColor(red:red, green:green, blue:blue, alpha:1.0)
}
编辑 Swift 3:
使用UITabBarController
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
没有嵌入式控制器
我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式控制器的情况下学习了一些技巧:
在您的 AppDelegate.swift 中添加此方法并在 didFinishLaunchingWithOptions
中调用它
func customizeAppearance() {
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UITabBar.appearance().barTintColor = UIColor.black
let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
UITabBar.appearance().tintColor = tintColor
}
感谢 @Utsav 我将以下子视图添加到我的 UITabBarController 并且现在似乎可以正常工作:
let view = UIView(frame:
CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)
)
view.backgroundColor = UIColor.orangeColor()
self.view.addSubview(view)
UITabBarController 似乎在 AppDelegate 中表现不佳。如果有人有更好的方法让我知道,但是,到目前为止,这是我已经想到的解决方案。
这就是我在没有在 NavBarController
中添加视图的 VC 中的做法
我希望状态栏的颜色与 VC 视图颜色相同,所以我只写了:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.grayColor()
self.navigationController?.navigationBar.clipsToBounds = true
}
试试吧。
西蒙在 swift 3
中的回答
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
我知道还有一种使用私有 api 的方法。当方向改变、键盘出现和视图向上移动时,这有一些好处。我用过,每次都很幸运(应用已在应用商店发布)。
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
Swift 3:
在您的 AppDelegate.swift 文件中,将下面的代码粘贴到您的 didFinishLaunchingWithOptions 方法中:
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = UIColor(red: 255/255, green: 130/255, blue: 0/255, alpha: 1.0) // Organge colour in RGB
self.window?.rootViewController?.view.addSubview(view)
这对我来说很好用!
我正在尝试使用以下方法将状态栏背景颜色填充为橙色
UINavigationBar.appearance().tintColor = UIColor.orangeColor()
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
但是,我得到了一个白色的状态栏,应该用橙色填充而不是按照这个例子:Customize navigation bar appearance with swift
我在 AppDelegate.swift 文件中 didFinishLaunchingWithOptions 方法下进行设置,以将其应用于整个应用程序。
我已将 info.plist 编辑为以下内容:View controller-based status bar appearance => NO
有谁知道我做错了什么吗?
编辑:我不确定它是否重要,但视图在 UITabBarController
中编辑 2:这实际上发生在所有视图中,而不仅仅是 UITabBarController。
编辑 3:谢谢 @Utsav Parikh
我现在在状态栏的顶部添加一个视图,当应用程序加载状态栏时,它会短暂显示橙色,但是一旦完成加载,它就会被推离视图并替换为通用的白色状态栏。 为什么会这样?
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window!.rootViewController!.view.addSubview(view)
编辑 Swift 3:
与UITabBarController
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
没有嵌入式控制器
我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式控制器的情况下学会了一些技巧:
在您的 AppDelegate.swift 中添加此方法并在 didFinishLaunchingWithOptions
中调用它func customizeAppearance() {
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UITabBar.appearance().barTintColor = UIColor.black
let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
UITabBar.appearance().tintColor = tintColor
}
在 AppDelegate
的 didFinishLaunchingWithOptions 中添加这段代码let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
view.backgroundColor=UIColor.orangeColor()
self.window.rootViewController.view.addSubview(view)
希望对您有所帮助....!!!
我认为你的最后一行正在恢复你的更改,试试这个:
override func viewWillAppear(animated: Bool) {
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
super.viewWillAppear(animated)
var nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.orangeColor()
nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
}
tintColor
和更改 UINavigationBar
的背景颜色有一个主要区别。我认为最好的方法是应用背景图像,由只有一种颜色的 1 像素正方形图像制成。
像那样:
let tabbarAndNavBarBkg = UIImage(named: "nav_tab")
UINavigationBar.appearance().setBackgroundImage(tabbarAndNavBarBkg, forBarMetrics: .Default)
或者您可以在 UIColor
上创建一个类别 return 一个 UIImage
给定一个 UIColor
实例,在 objC:
+ (UIImage *) imageWithColor:(UIColor*) color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return colorImage;
}
UINavigationBar.appereance()
适用于即将到来的 viewController,但不适用于当前显示的 rootViewController。为此,我在 didFinishLaunchingWithOptions
中添加了以下内容:
UINavigationBar.appearance().tintColor = myColor
let navigationController = UIApplication.sharedApplication().windows[0].rootViewController as! UINavigationController
navigationController.navigationBar.barTintColor = myColor
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : myTextColor]
navigationController.navigationBar.translucent = false
navigationController.setNeedsStatusBarAppearanceUpdate()
在你在 info.plist
中所做的之后:View controller-based status bar appearance
=> NO.
在 didFinishLaunchingWithOptions
下的 AppDelegate.swift
文件中添加此代码:
var navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = uicolorFromHex(0xffffff)
navigationBarAppearace.barTintColor = uicolorFromHex(0x2E9AFE)
// change navigation item title color
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
并且您可以 select 任何十六进制代码来选择颜色..!!享受..!!
抱歉,忘记使用十六进制码了,您也需要这个,所以请将此代码添加到 AppDelegate.swift
:
func uicolorFromHex(rgbValue:UInt32)->UIColor {
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0
let blue = CGFloat(rgbValue & 0xFF)/256.0
return UIColor(red:red, green:green, blue:blue, alpha:1.0)
}
编辑 Swift 3:
使用UITabBarController
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
没有嵌入式控制器
我意识到有些人来这里不仅是为了状态栏,实际上也是为了导航栏,所以我在没有任何嵌入式控制器的情况下学习了一些技巧:
在您的 AppDelegate.swift 中添加此方法并在 didFinishLaunchingWithOptions
中调用它func customizeAppearance() {
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UITabBar.appearance().barTintColor = UIColor.black
let tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
UITabBar.appearance().tintColor = tintColor
}
感谢 @Utsav 我将以下子视图添加到我的 UITabBarController 并且现在似乎可以正常工作:
let view = UIView(frame:
CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)
)
view.backgroundColor = UIColor.orangeColor()
self.view.addSubview(view)
UITabBarController 似乎在 AppDelegate 中表现不佳。如果有人有更好的方法让我知道,但是,到目前为止,这是我已经想到的解决方案。
这就是我在没有在 NavBarController
中添加视图的 VC 中的做法我希望状态栏的颜色与 VC 视图颜色相同,所以我只写了:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.grayColor()
self.navigationController?.navigationBar.clipsToBounds = true
}
试试吧。
西蒙在 swift 3
中的回答let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = .orange
self.view.addSubview(view)
我知道还有一种使用私有 api 的方法。当方向改变、键盘出现和视图向上移动时,这有一些好处。我用过,每次都很幸运(应用已在应用商店发布)。
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
Swift 3:
在您的 AppDelegate.swift 文件中,将下面的代码粘贴到您的 didFinishLaunchingWithOptions 方法中:
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.size.width, height: 20.0))
view.backgroundColor = UIColor(red: 255/255, green: 130/255, blue: 0/255, alpha: 1.0) // Organge colour in RGB
self.window?.rootViewController?.view.addSubview(view)
这对我来说很好用!