以编程方式更改iOS7状态栏颜色,中期运行?

问题描述:

我正在尝试在运行中更改状态栏颜色,即在加载控制器时没有。我更改了视图的背景颜色,因此我需要将其从黑色更改为白色,反之亦然。

I’m trying to change the Status Bar colour mid-run, i.e. not when a controller is loaded. I change the view’s background colour, so I need to change it from the black to white and vice versa.

我知道我可以使用 preferredStatusBarStyle 和plist中的设置,但据我所知,它只会在首次启动视图控制器时设置它。我想改变它,例如,当我按下按钮时。

I know that I can change it using preferredStatusBarStyle and the setting in the plist, but as far as I can see that’ll only set it on first launching the view controller. I’d like to change it, for instance, when I hit a button.

我可以这样做吗?


  • 转到您的应用程序Plist并将其添加为新行&将其设置为NO。

    • Go to your application Plist and add this as new row & set it as NO.

      View controller-based status bar appearance  NO
      


  • 添加bool以确定 UIStatusBar 颜色的状态&安培;添加一个Toggle方法

    Add a bool to determine state of UIStatusBar colour & add a Toggle method

@property(nonatomic) BOOL black;


-(void)toggleStatuSBar:(id)sender{

    if(black) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        black = NO;

    }else {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        black = YES;
    }
}

这里是一个样本ScreenShot

here is a Sample ScreenShot


  • 当菜单关闭时,颜色为白色。

  • When Menu is Closed, the colour is White.

当菜单打开时颜色为黑色

When Menu is Open The colour is Black

希望有所帮助。