iOS 7 and later: set status bar style per view controller

前端 未结 10 661
旧巷少年郎
旧巷少年郎 2020-12-02 07:43


I tried many ways to set the status bar style (default or lightcontent) but can\'t get it to work on a per view controller basis. I can set the status bar style for the

相关标签:
10条回答
  • 2020-12-02 08:02

    In the ViewController that you want to change the status bar's color

    - (void) viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }
    
    - (void) viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    
    0 讨论(0)
  • 2020-12-02 08:10

    Swift extension for this because I always forget how this works

    extension UIViewController {
        // utility to set the status bar appearance
        // Note: Make sure "View controller-based status bar appearance" is set to NO in your target settings or this won't work
        func setStatusBarForDarkBackground(dark: Bool) {
            UIApplication.sharedApplication().statusBarStyle = dark ? .LightContent : .Default
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    0 讨论(0)
  • 2020-12-02 08:12

    EDIT: This solution is deprecated on iOS 9. Please choose one of the other answers.

    With UIViewControllerBasedStatusBarAppearance set to NO, I was able to set the style to white text by using:

    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;
    

    This is because the text color on this style was white on iOS 6 and below.

    UPDATE: According to @jowie you can try that on iOS8:

    [UIApplication sharedApplication].statusBarStyle = UIBarStyleBlack;
    
    0 讨论(0)
  • 2020-12-02 08:13

    On viewDidLoad method, put this:

    Objective C

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self setNeedsStatusBarAppearanceUpdate];
    

    Swift

    UIApplication.shared.statusBarStyle = .lightContent
    self.setNeedsStatusBarAppearanceUpdate()
    
    0 讨论(0)
提交回复
热议问题