如何在iOS 7上更改状态栏背景颜色和文本颜色?

Rej*_*jan 82 uicolor ios ios7 ios-statusbar

我目前的应用程序在iOS 5和6上运行.

导航栏为橙色,状态栏为黑色背景颜色,白色为文本颜色.但是,当我在iOS 7上运行相同的应用程序时,我发现状态栏看起来是透明的,具有与导航栏相同的橙色背景颜色,状态栏文本颜色为黑色.

由于这个原因,我无法区分状态栏和导航栏.

如何使状态栏看起来与iOS 5和6中的状态相同,即黑色背景颜色和白色文本颜色?我该如何以编程方式执行此操作?

War*_*shi 172

我不得不尝试寻找其他方法.哪个不涉及addSubview窗口.因为当键盘呈现时我正在向上移动窗口.

目标C.

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}
Run Code Online (Sandbox Code Playgroud)

迅速

func setStatusBarBackgroundColor(color: UIColor) {

    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
        return
    }

    statusBar.backgroundColor = color
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

func setStatusBarBackgroundColor(color: UIColor) {

    guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }

    statusBar.backgroundColor = color
}
Run Code Online (Sandbox Code Playgroud)

调用此表单application:didFinishLaunchingWithOptions对我有用.

注意我们在应用商店中有一个具有此逻辑的应用.所以我想应用商店政策是可以的.


编辑:

使用风险由您自己承担.组成评论者@Sebyddd

我有一个应用程序拒绝原因,而另一个被接受就好了.他们确实将其视为私有API使用,因此您在审核过程中会受到好运:) - Sebyddd

  • 与接受的解决方案不同,这在您更改方向时也有效.谢谢! (5认同)
  • 是不是私有API使用? (5认同)
  • 此解决方案存在问题,当您双击主页按钮时,此状态状态栏颜色将消失. (3认同)
  • 我有一个应用程序拒绝原因,而另一个被接受就好了.他们确实将其视为私有API使用,因此您在审核过程中会遇到运气:) (2认同)
  • 在iOS 13上不起作用。UIApplication上的-statusBar或-statusBarWindow应用程序:此代码“必须更改”,因为不再有状态栏或状态栏窗口。而是在窗口场景上使用statusBarManager对象。 (2认同)

Sha*_*bal 107

转到您的应用程序info.plist

1)设置View controller-based status bar appearanceNO
2)设置Status bar styleUIStatusBarStyleLightContent

然后转到您的应用程序委托并粘贴以下代码,您可以在其中设置Windows的RootViewController.

#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];
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • 这不考虑轮换 (5认同)
  • 最好使用UIScreen宽度:`UIView*view = [[UIView alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen] .bounds.size.width,20)];` (4认同)
  • @learner转到info.plist然后选择任何行.你会看到一个+号.单击加号,然后从下拉菜单中看到"状态栏样式"选项.选择它.并粘贴`UIStatusBarStyleLightContent`作为其值. (2认同)
  • 设置框架的更简洁方法是使用`UIApplication.sharedApplication().statusBarFrame` (2认同)

Mur*_*m K 27

1)在plist中将UIViewControllerBasedStatusBarAppearance设置为YES

2)在viewDidLoad中做一个 [self setNeedsStatusBarAppearanceUpdate];

3)添加以下方法:

 -(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 
Run Code Online (Sandbox Code Playgroud)

更新:
还要检查开发人员指南到ios-7-status-bar

  • 啊,我发现了这个问题.Apple的UINavigationController抓取通知 - 即您的答案仅适用于视图控制器是顶级控制器,没有容器(没有标签栏,没有导航栏等)的情况. (3认同)
  • 使用Storyboard + NavigationController时的特殊情况.做#1以上.接下来,为UINavigationController创建一个子类(将其命名为myNavController).在Storyboard中,将NavigationController的类设置为"myNavController".在myNavController.m中,执行上面的#2和#3.现在将在您的子类中调用#3中的方法(设置要观察的日志或断点). (3认同)

Tej*_*ina 25

在iOS 7中处理状态栏的背景颜色时,有两种情况

案例1:使用导航栏查看

在这种情况下,请在viewDidLoad方法中使用以下代码

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor = [UIColor yellowColor];
 [self.navigationController.navigationBar addSubview:statusBarView];
Run Code Online (Sandbox Code Playgroud)

案例2:没有导航栏的视图

在这种情况下,请在viewDidLoad方法中使用以下代码

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor  =  [UIColor yellowColor];
 [self.view addSubview:statusBarView];
Run Code Online (Sandbox Code Playgroud)

来源链接http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html


Kru*_*nal 15

您可以在应用程序启动期间或视图控制器的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
    }

}
Run Code Online (Sandbox Code Playgroud)



结果如下:

在此输入图像描述


以下是有关状态栏更改的Apple指南/说明.状态栏中仅允许黑暗和浅色(同时为黑色).

这是 - 如何更改状态栏样式:

如果要设置状态栏样式,则应用程序级别将设置UIViewControllerBasedStatusBarAppearanceNO".plist"文件.

如果您要在视图控制器级别设置状态栏样式,请按照下列步骤操作:

  1. 如果需要仅在UIViewController级别设置状态栏样式,请在文件中设置UIViewControllerBasedStatusBarAppearanceto .YES.plist
  2. 在viewDidLoad中添加功能 - setNeedsStatusBarAppearanceUpdate

  3. 覆盖视图控制器中的preferredStatusBarStyle.

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}
Run Code Online (Sandbox Code Playgroud)


Gab*_*lla 14

在iOS 7中,状态栏没有背景,因此如果您在其后面放置一个黑色的20px高视图,您将获得与iOS 6相同的结果.

另外,您可能需要阅读iOS 7 UI过渡指南以获取有关该主题的更多信息.

  • 加布里埃尔,你能否提供代码如何将20px高视图放在后面? (2认同)

san*_*ana 8

在ViewDidLoad方法中写下这个:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;
}
Run Code Online (Sandbox Code Playgroud)

它在一定程度上修复了我和其他UI错位的状态栏颜色.


小智 6

对于背景,您可以轻松添加视图,例如:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
view.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
[navbar addSubview:view];
Run Code Online (Sandbox Code Playgroud)

其中"navbar"是UINavigationBar.

我希望它可以帮到你!

  • 你的前两行是正确的..但最后一行应该是[navigationController.view addSubview:view]; 它应该在UINavigationController的视图中添加而不是UINavigationBar的视图,因为它将在20 px状态栏不重叠状态栏后添加视图. (4认同)

ben*_*ree 6

只是为了添加Shahid的答案 - 您可以使用此方法考虑方向更改或不同的设备(iOS7 +):

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...

  //Create the background
  UIView* statusBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 20)];
  statusBg.backgroundColor = [UIColor colorWithWhite:1 alpha:.7];

  //Add the view behind the status bar
  [self.window.rootViewController.view addSubview:statusBg];

  //set the constraints to auto-resize
  statusBg.translatesAutoresizingMaskIntoConstraints = NO;
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[statusBg(==20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(statusBg)]];
  [statusBg.superview setNeedsUpdateConstraints];
  ...
}
Run Code Online (Sandbox Code Playgroud)


Fat*_*tie 6

这是一个完整的,复制和粘贴的解决方案,带有

绝对正确的解释

涉及的每个问题.

感谢Warif Akhand Rishi!

关于keyPath的惊人发现statusBarWindow.statusBar.好的.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // handle the iOS bar!

    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // "Status Bar Style" refers to the >>>>>color of the TEXT<<<<<< of the Apple status bar,
    // it does NOT refer to the background color of the bar. This causes a lot of confusion.
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<

    // our app is white, so we want the Apple bar to be white (with, obviously, black writing)

    // make the ultimate window of OUR app actually start only BELOW Apple's bar....
    // so, in storyboard, never think about the issue. design to the full height in storyboard.
    let h = UIApplication.shared.statusBarFrame.size.height
    let f = self.window?.frame
    self.window?.frame = CGRect(x: 0, y: h, width: f!.size.width, height: f!.size.height - h)

    // next, in your plist be sure to have this: you almost always want this anyway:
    // <key>UIViewControllerBasedStatusBarAppearance</key>
    // <false/>

    // next - very simply in the app Target, select "Status Bar Style" to Default.
    // Do nothing in the plist regarding "Status Bar Style" - in modern Xcode, setting
    // the "Status Bar Style" toggle simply sets the plist for you.

    // finally, method A:
    // set the bg of the Apple bar to white.  Technique courtesy Warif Akhand Rishi.
    // note: self.window?.clipsToBounds = true-or-false, makes no difference in method A.
    if let sb = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
        sb.backgroundColor = UIColor.white
        // if you prefer a light gray under there...
        //sb.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.9, alpha: 1)
    }

    /*
    // if you prefer or if necessary, method B:
    // explicitly actually add a background, in our app, to sit behind the apple bar....
    self.window?.clipsToBounds = false // MUST be false if you use this approach
    let whiteness = UIView()
    whiteness.frame = CGRect(x: 0, y: -h, width: f!.size.width, height: h)
    whiteness.backgroundColor = UIColor.green
    self.window!.addSubview(whiteness)
    */

    return true
}
Run Code Online (Sandbox Code Playgroud)


小智 5

斯威夫特 4:

// 改变状态栏背景颜色

let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView

statusBar?.backgroundColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)