我想把状态栏的颜色改成蓝色或者其他颜色。
这个能做到吗?还是苹果不允许这样做?
我想把状态栏的颜色改成蓝色或者其他颜色。
这个能做到吗?还是苹果不允许这样做?
注意:此解决方案在 iOS 13 及更高版本下无法使用。
首先,在 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
}
The output screenshot is below
不,使用现成的公共API是不可能的。
但是随着iOS 7
的发布,你可以改变状态栏的外观。因此,我在这里发布我的解决方法。
通过覆盖preferredStatusBarStyle
,可以从单个视图控制器中更改状态栏的外观:
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
另外,您可以使用UIApplication statusBarStyle
方法来设置状态栏样式。为此,请插入一个名为“View controller-based status bar appearance”的新键,并将值设置为NO。
通过禁用“View controller-based status bar appearance”,您可以使用以下代码设置状态栏样式。
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UINavigationBar
属性的色调颜色。[[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
}
}
这里是更改状态栏样式的 苹果指南/说明 ,只允许在状态栏中使用深色和浅色(白色和黑色)。
以下是如何更改状态栏样式:
如果您想要在应用程序级别设置状态栏样式,请在 `.plist' 文件中将 UIViewControllerBasedStatusBarAppearance
设置为 NO
。
如果您想要在视图控制器级别设置状态栏样式,请按照以下步骤进行:
.plist
文件中将 UIViewControllerBasedStatusBarAppearance
设置为 YES
。在 viewDidLoad 中添加函数 - setNeedsStatusBarAppearanceUpdate
重写您的视图控制器中的 preferredStatusBarStyle 方法。
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
根据状态栏样式设置水平,设置.plist的值。
UIView
,将其添加到你的视图控制器根视图中,作为人工状态栏背景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,所以您可以放心使用。 :-)
我制作了这个扩展来改变状态栏的颜色。
public extension UIViewController {
func setStatusBar(color: UIColor) {
let tag = 12321
if let taggedView = self.view.viewWithTag(tag){
taggedView.removeFromSuperview()
}
let overView = UIView()
overView.frame = UIApplication.shared.statusBarFrame
overView.backgroundColor = color
overView.tag = tag
self.view.addSubview(overView)
}
}
以下是在视图控制器中使用的示例:
setStatusBar(color: .red)
翻译:设置状态栏颜色为红色。