更改状态栏的颜色
我试图将状态栏的颜色更改为蓝色或其他颜色。
这可能吗,还是Apple不允许这样做?
首先在Plist集中View controller-based status bar appearance
为NO
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) { statusBar.backgroundColor = UIColor.blue } UIApplication.shared.statusBarStyle = .lightContent return true }
输出屏幕截图如下
不,使用现成的公共API是不可能的。
但随着iOS 7
的发布,您可以更改状态栏的外观。 因此,我发布了我的解决方法。
通过覆盖preferredStatusBarStyle
从单个视图控制器:
-(UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
或者,您可以使用UIApplication statusBarStyle
方法设置状态栏样式。 为此,请插入名为“查看基于控制器的状态栏外观”的新密钥,并将值设置为NO。
通过禁用“查看基于控制器的状态栏外观”,可以使用以下代码设置状态栏样式。
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
最后,更改UINavigationBar
属性tint颜色,如下所示
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
您可以在应用程序启动期间或视图控制器的viewDidLoad期间为状态栏设置背景颜色。
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 } }
结果如下:
以下是有关状态栏更改的Apple指南/说明 。 状态栏中只允许黑暗和浅色(黑色和黑色)。
这是 – 如何更改状态栏样式:
如果要设置状态栏样式,应用程序级别,请在“.plist”文件中将UIViewControllerBasedStatusBarAppearance
设置为NO
。
如果您要设置状态栏样式,请在视图控制器级别,然后按照下列步骤操作:
- 如果需要仅在UIViewController级别设置状态栏样式,请在
.plist
文件中将UIViewControllerBasedStatusBarAppearance
设置为YES
。 -
在viewDidLoad中添加函数
setNeedsStatusBarAppearanceUpdate
-
覆盖视图控制器中的preferredStatusBarStyle。
–
override func viewDidLoad() { super.viewDidLoad() self.setNeedsStatusBarAppearanceUpdate() } override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
根据状态栏样式设置级别设置.plist的值。
这是我的解决方法: 创建一个UIView
,将其添加到视图控制器的根视图作为人工状态栏背景
1.创造一个UIView
// status bar's height is 20.0pt CGRect frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 20.0); UIView *fakeStatusBarBG = [[UIView alloc] initWithFrame:frame]; fakeStatusBarBG.backgroundColor = [UIColor yourColor];
2.将其添加到视图控制器的根视图中
// self is your view controller, make sure fakeStatusBarBG is the top subview in your view hierarchy [self.view insertSubview:fakeStatusBarBG aboveSubview:yourTopSubview];
你去吧。
3.(附加)更改状态栏上的内容颜色,仅白色或黑色。
- (UIStatusBarStyle)preferredStatusBarStyle { if (youWantWhiteColor) { return UIStatusBarStyleLightContent; } return UIStatusBarStyleDefault; }
此解决方法不使用私有API,因此您是安全的。 🙂