更改 iOS 7 之后的状态栏背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21180173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:09:24  来源:igfitidea点击:

Change the status bar background color color past iOS 7

ioscocoa-touchuistatusbar

提问by Krunal

I want to change background color of status bar on iOS 7, and I'm using this code:

我想在 iOS 7 上更改状态栏的背景颜色,我正在使用以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [UIApplication sharedApplication].statusBarHidden = NO;

        self.window.clipsToBounds = YES;
        [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
        self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
        self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);

        ...
        ...


}

When I write this it shows the status bar with a black background; I want it to have a red background.

当我写这篇文章时,它会显示带有黑色背景的状态栏;我希望它有一个红色的背景。

How can change the color of the status bar to have a red background instead of black?

如何将状态栏的颜色更改为红色背景而不是黑色?

采纳答案by Sahil Mahajan

In iOS 7 and later, the status bar is transparent. Set the backgroundColorof your view to the color you want for the status bar.

在 iOS 7 及更高版本中,状态栏是透明的。将backgroundColor视图的设置为状态栏所需的颜色。

Or, you can add a 20px-high subview with red color at the top of your view.

或者,您可以在视图顶部添加一个 20 像素高的红色子视图。

See the Apple Transition Guidefor more.

有关更多信息,请参阅Apple 过渡指南

Also, make sure that your preferredStatusBarStyleis UIStatusBarStyleLightContent. and in your Info.plist set "View controller-based status bar appearance" to "NO".

另外,请确保您preferredStatusBarStyleUIStatusBarStyleLightContent. 并在您的 Info.plist 中将“查看基于控制器的状态栏外观”设置为“否”。

回答by Teja Kumar Bethina

While handling the background color of status bar in iOS 7, there are 2 cases

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

Case 1: View with Navigation Bar

案例 1:使用导航栏查看

In this case use the following code in your viewDidLoad method

在这种情况下,在您的 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];

Case 2: View without Navigation Bar

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

In this case use the following code in your viewDidLoad method

在这种情况下,在您的 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];

回答by Subramanyam Subbu

Objective c

目标c

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

    [[UIApplication sharedApplication] setStatusBarHidden:false];
    UIView *statusBar=[[UIApplication sharedApplication] valueForKey:@"statusBar"];
    statusBar.backgroundColor = [UIColor redColor];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];



    return YES;
}

This is working for me. I hope this will help you too.

这对我有用。我希望这也能帮助你。

回答by Alan Feng

for what @Teja Kumar Bethina provided is helpful, but it's better to get the height of the statusBar from UIApplication singleton like below:

@Teja Kumar Bethina 提供的内容很有帮助,但最好从 UIApplication 单例中获取 statusBar 的高度,如下所示:

UIApplication *app = [UIApplication sharedApplication];

UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -app.statusBarFrame.size.height, self.view.bounds.size.width, app.statusBarFrame.size.height)];
    statusBarView.backgroundColor = [UIColor blackColor];

回答by Madhu

in AppDelegate use

在 AppDelegate 中使用

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

and in project info.plist file

并在项目 info.plist 文件中

set flag NO to View controller-based status bar appearance in app.

将标志 NO 设置为在应用程序中查看基于控制器的状态栏外观。

回答by Deepak Carpenter

Call this function from ViewDidLoad passing with your status bar color.

从带有状态栏颜色的 ViewDidLoad 调用此函数。

func setStatusBarBackgroundColor(color: UIColor)  
{
    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView 
    else 
    {
    return
    }
    statusBar.backgroundColor = color
}

Hope It'll help.

希望它会有所帮助。

回答by maninvan

Here's a swift solution that uses auto layout, If your application is in portrait or landscape the bar will be the correct width. It's added to the navigation bar view as a subview hence it's offset by -20. You could add it to your navigationbar's parent view, and then the offset is not required. This example will also set the colour to be the same as the navigation bar.

这是一个使用自动布局的快速解决方案,如果您的应用程序是纵向或横向,条形将是正确的宽度。它作为子视图添加到导航栏视图中,因此偏移了 -20。您可以将其添加到导航栏的父视图中,然后不需要偏移量。此示例还将设置颜色与导航栏相同。

Hope that helps :)

希望有帮助:)

    // Add colored opaque view behind the status bar view
    let statusBarView = UIView()
    statusBarView.backgroundColor = navigationBar.backgroundColor
    statusBarView.translatesAutoresizingMaskIntoConstraints = false
    navigationBar.addSubview(statusBarView)

    let views = ["statusBarView": statusBarView]
    let hConstraint = "H:|-0-[statusBarView]-0-|"
    let vConstraint = "V:|-(-20)-[statusBarView(20)]"

    var allConstraints = NSLayoutConstraint.constraintsWithVisualFormat(hConstraint,
            options: [], metrics: nil, views: views)

    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat(vConstraint,
            options: [], metrics: nil, views: views)

    NSLayoutConstraint.activateConstraints(allConstraints)

回答by Shubham

if your application is navigation based then you can set status bar background color red like this:

如果您的应用程序是基于导航的,那么您可以像这样设置状态栏背景颜色为红色:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

may it will help.

可能会有所帮助。