iOS 7: modal view controller status bar is wrong color but normal view controllers are correct

前端 未结 9 1476
慢半拍i
慢半拍i 2021-02-05 06:20

I have an issue in iOS7 where a normal UINavigationController pushed view controller has the correct status bar text color for the UINavigationController navbar color (which is

9条回答
  •  甜味超标
    2021-02-05 07:01

    You can redefine the preferredStatusBarStyle method in your navigation controller class

    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return UIStatusBarStyleLightContent;
    // or UIStatusBarStyleBlackOpaque, UIStatusBarStyleBlackTranslucent, or UIStatusBarStyleDefault
    
    }
    

    and you can also define a "view did load method" to set custom colors you want

    - (void) viewDidLoad
    {
        UIColor *barColor = [UIColor whitecolor];
    
        UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0.f, -20.f, 320.f, 64.f)];
        colorView.opaque = NO;
        colorView.alpha = .5f;
        colorView.backgroundColor = barColor;
    
        self.navigationBar.barTintColor = barColor;
        self.navigationBar.tintColor = [UIColor whiteColor];
        [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
        /*self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
         self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
         [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
         self.navigationController.navigationBar.translucent = NO;*/
    
        [self.navigationBar.layer insertSublayer:colorView.layer atIndex:0];
    
    }
    

提交回复
热议问题