csdnceshi59
ℙℕℤℝ
2013-07-16 14:11
浏览 774
已采纳

如何改变 iOS 系统中状态栏的文字颜色

My application has a dark background, but in iOS 7 the status bar became transparent. So I can't see anything there, only the green battery indicator in the corner. How can I change the status bar text color to white like it is on the home screen?

转载于:https://stackoverflow.com/questions/17678881/how-to-change-status-bar-text-color-in-ios

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

30条回答 默认 最新

  • csdnceshi72
    谁还没个明天 2013-07-21 03:36
    已采纳
    1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file.

    2. In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];

    3. Add the following method:

      - (UIStatusBarStyle)preferredStatusBarStyle
      { 
          return UIStatusBarStyleLightContent; 
      }
      

    Note: This does not work for controllers inside UINavigationController, please see Tyson's comment below :)

    Swift 3 - This will work controllers inside UINavigationController. Add this code inside your controller.

    // Preferred status bar style lightContent to use on dark background.
    // Swift 3
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    点赞 评论
  • csdnceshi51
    旧行李 2013-07-16 14:24

    This is documented in the iOS 7 UI Transition Guide, which you need an Apple developer ID to access directly. The relevant excerpt:

    Because the status bar is transparent, the view behind it shows through. [...] Use a UIStatusBarStyle constant to specify whether the statusbar content should be dark or light:

    UIStatusBarStyleDefault displays dark content. [...]

    UIStatusBarStyleLightContent displays light content. Use when dark content is behind the status bar.

    Also possibly of interest:

    In iOS 7, you can control the style of the status bar from an individual vew controller and change it while the app runs. To opt in to this behavior, add the UIViewControllerBasedStatusBarAppearance key to an app's Info.plist file and give it the value YES.

    I'd definitely recommend having a look through the document, which, again, you can access with your Apple developer ID.

    点赞 评论
  • csdnceshi76
    斗士狗 2013-08-12 10:35

    Alternatively, you can opt out of the view-controller based status bar appearance:

    1. Set View controller-based status bar appearance to NO in your Info.plist.
    2. Call [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    Note: This method has been deprecated in iOS9. Use preferredStatusBarStyle on the UIViewController instead. (see Apple Developer Library)

    点赞 评论
  • csdnceshi78
    程序go 2013-08-19 00:41

    If you have an embedded navigation controller created via Interface Builder, be sure to set the following in a class that manages your navigation controller:

    -(UIStatusBarStyle)preferredStatusBarStyle{ 
        return UIStatusBarStyleLightContent; 
    } 
    

    That should be all you need.

    点赞 评论
  • csdnceshi68
    local-host 2013-09-11 04:47

    You can do this without writing any line of code!
    Do the following to make the status bar text color white through the whole app

    On you project plist file:

    • Status bar style: Transparent black style (alpha of 0.5)
    • View controller-based status bar appearance: NO
    • Status bar is initially hidden: NO
    点赞 评论
  • csdnceshi61
    derek5. 2013-09-12 16:42

    For me, nothing happened with using all the things in the other answers (and from other sources/documentation). What did help was to set the Navigation Bar Style to "Black" in the XIB. This changed the text to white without any code at all.

    Enter image description here

    点赞 评论
  • csdnceshi77
    狐狸.fox 2013-09-13 08:28

    This works in Golden Master iOS 7 and Xcode 5 GM seed and iOS7 SDK released on September 18th, 2013 (at least with navigation controller hidden):

    1. Set the UIViewControllerBasedStatusBarAppearance to NO in the Info.plist.

    2. In ViewDidLoad method or anywhere, where do you want to change status bar style: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    点赞 评论
  • csdnceshi55
    ~Onlooker 2013-09-16 06:43

    Answer updated for for Xcode GM Seed:

    1. In Info.plist put View controller-based status bar appearance as NO

    2. In the project, set:

      Enter image description here

    3. In ViewDidLoad:

      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    点赞 评论
  • csdnceshi79
    python小菜 2013-09-22 13:54

    I did some things different and it works for me.

    With no changes in code, I did config my .plist file like this:

    • View controller-based status bar appearance > NO
    • Status bar style > UIStatusBarStyleLightContent (simple string)

    I hope it helps.

    edit

    For each view controller I change the "status bar"'s Simulated Metrics property, in storyboard, from "inferred" to "Light Content"

    点赞 评论
  • csdnceshi52
    妄徒之命 2013-09-23 05:11

    Note: Most upvoted answer does not work for iOS 7 / 8

    In Info.plist set 'View controller-based status bar appearance' as NO

    In AppDelegate add

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    

    to

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

    This solution works for iOS 7 / 8

    点赞 评论
  • csdnceshi58
    Didn"t forge 2013-09-27 08:16

    In case your UIViewController is inside a UINavigationController you will have to set the BarStyle:

    -[UINavigationBar setBarStyle:UIBarStyleBlack]
    

    Original Answer is here

    https://devforums.apple.com/message/844264#844264

    点赞 评论
  • csdnceshi69
    YaoRaoLov 2013-09-27 08:49

    No need do some extra , just write this code in your viewController and get status bar color white

    - (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;}
    
    点赞 评论
  • weixin_41568196
    撒拉嘿哟木头 2013-10-16 18:21

    The key to making this work is that only the fullscreen view controller get's to dictate the style of the status bar.

    If you are using a navigation controller and want to control the status bar on a per view controller basis, you'll want to subclass UINavigationController and implement preferredStatusBarStyle such that it returns the topViewController's preference.

    Make sure you change the class reference in your storyboard scene fromUINavigationController to your subclass (e.g. MyNavigationController in the example below).

    (The following works for me. If your app is TabBar based, you'll want to do something similar by subclassing the UITabBarController but I haven't tried that out).

    @interface MyNavigationController : UINavigationController
    
    @end
    
    @implementation MyNavigationController
    
    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return self.topViewController.preferredStatusBarStyle;
    }
    
    @end
    
    点赞 评论
  • weixin_41568126
    乱世@小熊 2013-11-10 20:07

    Simply calling

    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
    

    in the

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

    method of my AppDelegate works great for me in iOS7.

    点赞 评论
  • csdnceshi75
    衫裤跑路 2013-12-10 13:47

    iOS 7 allows individual view controllers to determine the appearance of the status bar, as described by the Apple developer documentation:

    iOS 7 gives view controllers the ability to adjust the style of the status bar while the app is running. A good way to change the status bar style dynamically is to implement preferredStatusBarStyle and—within an animation block—update the status bar appearance and call setNeedsStatusBarAppearanceUpdate.

    Setting the status bar appearance globally is a two-step process.

    First, you need to tell iOS that you don't want to set the status bar appearance on a view-by-view basis.

    Then you need to take charge and actually set the new global status bar style.

    To disable view-by-view status bar control, you'll need to set the View controller-based status bar appearance property in Info.plist.

    Open the Project Navigator and select the project for your iOS app, then select the Info tab.

    Hover over a row, then click the plus sign that appears to add a new property to your .plist.

    Enter View controller-based status bar appearance in the Key field, then make sure the Type field is set to Boolean. Finally, enter NO in the Value field.

    To set a global style for the status bar, add another property under the Info tab with a key of Status bar style, a Type of String and a Value of Opaque black style.

    Here's a blog post with a little more detail and some sample code:

    http://codebleep.com/setting-the-status-bar-text-color-in-ios-7/

    点赞 评论
  • weixin_41568208
    北城已荒凉 2013-12-21 13:55

    Just two steps as following:

    Step 1:

    Under the Info tab of the project target, Add Row:

    UIViewControllerBasedStatusBarAppearance, set value NO.

    Step 2:

    In the project AppDelegate.m:

    - (BOOL)application:(UIApplication *)application 
            didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
        …
        [application setStatusBarStyle:UIStatusBarStyleLightContent];
        …
    }
    
    点赞 评论
  • csdnceshi65
    larry*wei 2013-12-29 11:28

    None of that worked for me, so here is a working solution...

    In Info.plist, add a row:

    UIViewControllerBasedStatusBarAppearance, and set the value NO.

    Then in AppDelegate in didFinishLaunchingWithOptions, add these rows:

    [application setStatusBarHidden:NO];
    [application setStatusBarStyle:UIStatusBarStyleLightContent];
    
    点赞 评论
  • csdnceshi52
    妄徒之命 2014-01-10 12:40

    Well, this is really working like a piece of cake for me.

    Go to your app's info.plist.

    1. Set View controller-based status bar appearance to NO
    2. Set Status bar style to UIStatusBarStyleLightContent

    Then go to your app's delegate and paste in the following code where you set your windows's 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 colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
        [self.window.rootViewController.view addSubview:view];
    }
    

    Bingo. It's working for me.

    点赞 评论
  • csdnceshi62
    csdnceshi62 2014-01-16 17:34

    Simply In Appdelegate

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    点赞 评论
  • weixin_41568208
    北城已荒凉 2014-08-20 22:34

    I'm using Xcode 6 beta 5 on a Swift project, for an iOS 7 app.

    Here is what I did, and it works:

    info.plist:

    Enter image description here

    点赞 评论
  • weixin_41568131
    10.24 2014-09-06 04:09

    In AppDelegate.m, add the following.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    } 
    

    And in the Plist file, set 'View controller-based status bar appearance' to NO.

    点赞 评论
  • csdnceshi74
    7*4 2014-10-08 14:17

    Just to summarize, edit your project Info.plist and add:

    View controller-based status bar appearance : NO

    Status bar style : Opaque black style

    or if you have raw key/value plist

    UIViewControllerBasedStatusBarAppearance : NO

    UIStatusBarStyle : Opaque black style

    点赞 评论
  • csdnceshi78
    程序go 2015-02-05 12:28

    If you still want to use View controller-based status bar appearance in info.plist set to YES, meaning that you can change the statusbar for each view-controller, use the following for white text in the status-bar in ViewDidLoad:

    [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlackTranslucent];
    
    点赞 评论
  • weixin_41568174
    from.. 2015-03-30 05:06

    in info.plist set the field value NO View controller-based status bar appearance and set statusbar style light in target > general setting.

    点赞 评论
  • csdnceshi76
    斗士狗 2015-06-30 17:38

    Let me give you a complete answer to your question. Changing the status bar text color is very easy but its a little confusing in iOS 7 specially for newbies.

    If you are trying to change the color from black to white in Storyboard by selecting the view controller and going to Simulated Metrics on the right side, it won't work and i don't know why. It should work by changing like this but any how.

    Secondly, you won't find UIViewControllerBasedStatusBarAppearance property in your plist but by default its not there. You have to add it by yourself by clicking on the + button and then set it to NO.

    ios 7 status bar text color

    Lastly, you have to go to your AppDelegate.m file and add the following in didFinishLaunchingWithOptions method, add the following line:

         [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    

    This will change the color to white for all your view controllers. Hope this helps!

    点赞 评论
  • csdnceshi65
    larry*wei 2015-09-17 13:18

    I think all the answers do not really point the problem because all of them work in specific scenarios. But if you need to cover all the cases follow the points bellow:

    Depending on where you need the status bar light style you should always have in mind these 3 points:

    1)If you need the status bar at the launch screen or in other places, where you can't control it (not in view controllers, but rather some system controlled elements/moments like Launch Screen) You go to your project settings Project settings

    2) if you have a controller inside a navigation controller You can change it in the interface builder as follows:

    a) Select the navigation bar of your navigation controller Select the navigation bar of your navigation controller

    b) Then set the style of the navigation bar to "Black", because this means you'll have a "black" -> dark background under your status bar, so it will set the status bar to white

    enter image description here

    Or do it in code as follows

    navigationController?.navigationBar.barStyle = UIBarStyle.Black
    

    3) If you have the controller alone that needs to have it's own status bar style and it's not embedded in some container structure as a UINavigationController

    Set the status bar style in code for the controller:

    Setting the status bar style in code

    点赞 评论
  • csdnceshi78
    程序go 2016-01-10 19:30

    If you want the same result with Swift, you can use this code in your AppDelegate.swift file :

    UINavigationBar.appearance().barStyle = .BlackTranslucent
    

    And the text of your status bar will be white :-) !

    点赞 评论
  • csdnceshi78
    程序go 2016-02-09 09:00

    You dont need to do any code for this

    You need to add "View controller-based status bar appearance" key in info.plist as follows: enter image description here

    & set its value type to Boolean & value to NO. Then click on project settings,then click on General Tab & under Deployment Info set the preferred status bar style to .Light as follows:

    enter image description here

    Thats it.

    点赞 评论
  • csdnceshi53
    Lotus@ 2016-05-31 01:46
    1. Go to Project -> Target,

    2. Then set Status Bar Style to Light. It makes status-bar white from the launch screen. Project Setting

    3. Then set View controller-based status bar appearance equal to NO in Info.plist.

    点赞 评论
  • csdnceshi55
    ~Onlooker 2017-03-27 22:23

    Change in info PLIST In Swift 3 is very easy just with 2 steps. Go to your info.plist and change the key View controller-based status bar appearance to "NO". Then in the Appdelegate just add this line in didfinishlaunchingwithoptions method

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            UIApplication.shared.statusBarStyle = .lightContent
            return true
        }
    
    点赞 评论

相关推荐