简体   繁体   English

在iOS7中将UIStatusBar的背景色设置为黑色

[英]Set the background color of UIStatusBar as black in ios7

I don't know how can I set the background color of UIStatusBar as black in iOS7 while background color of navigation bar is not black.and as in iOS7 navigationbar is tranparent so status bar get backgroundcolor from background color of UINavigationBar.and text over statusbar should be of white color.I have used UIStatusBarStyleBlackOpaque but it is also deprecated in iOS7.I know I can change the textcolor of statusbar as white by setting StatusBarStyle as UIStatusBarStyleLightContent . 我不知道如何在iOS7中将UIStatusBar的背景色UIStatusBar为黑色,而导航栏的背景色不是黑色。并且在iOS7中,导航条是透明的,因此状态栏从UINavigationBar的背景色中获取背景色。应该是白色的。我使用过UIStatusBarStyleBlackOpaque,但在iOS7中也已弃用。我知道可以通过将StatusBarStyle设置为UIStatusBarStyleLightContent来将statusbar的文本颜色更改为白色。

And for setting the background color of UIStatusBar as black i have used below code:- 为了将UIStatusBar的背景色设置为黑色,我使用了以下代码:-

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        UIView *addStatusBar = [[UIView alloc] init];
        addStatusBar.frame = CGRectMake(0, 0, 320, 20);
        addStatusBar.backgroundColor = [UIColor blackColor];
        [self.window.rootViewController.view addSubview:addStatusBar];
    }

By above code i have manually added uiview in place of status bar of needed color. 通过以上代码,我手动添加了uiview来代替所需颜色的状态栏。 The problem is that by this uistatusbar is gone to back of the black view ie addStatusBar and thus text over status bar is hidden. 问题在于,此uistatusbar移到了黑色视图的后面,即addStatusBar,因此状态栏上的文本被隐藏了。

How to solve it? 怎么解决呢?

Please Help !! 请帮忙 !! Thanks in Advance !! 提前致谢 !!

I Just added this line to make navigationbar non-translucent. 我刚刚添加了这一行,以使Navigationbar不透明。

[navC.navigationBar setTranslucent:NO]; [navC.navigationBar setTranslucent:NO];

and use the same above code to add uiview in place of statusbar with needed color.with lightcontentstyle to show the text in white color. 并使用与上述相同的代码在状态栏上添加所需颜色的uiview。lightcontentstyle以白色显示文本。 It solved my problem. 它解决了我的问题。

Goto your app info.plist 转到您的应用程序info.plist

1) Set View controller-based status bar appearance to NO 2) Set Status bar style to UIStatusBarStyleLightContent 1)将基于View控制器的状态栏外观设置为NO 2)将状态栏样式设置为UIStatusBarStyleLightContent

Then Goto your app delegate and paste the following code where you set your Windows's RootViewController. 然后转到您的应用程序委托,并将以下代码粘贴到您设置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,320, 20)];
    view.backgroundColor=[UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}

Hope it helps. 希望能帮助到你。

You can set/change background color for status bar during application launch or during viewDidLoad of your view controller. 您可以在应用程序启动或视图控制器的viewDidLoad期间设置/更改状态栏的背景颜色。

I tried with iOS 11 (with Swift 4) here: 我在这里尝试使用iOS 11(使用Swift 4):

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.cyan
        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.cyan
    }

}



Here is result: 结果如下:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM