1. 程式人生 > >ios 狀態列statusBar的背景顏色和字型顏色設定

ios 狀態列statusBar的背景顏色和字型顏色設定

假如我想讓狀態列顏色設定成紅色,字型仍為黑色,可以在需要顯示的那一頁進行如下設定:(最好寫在viewWillAppear裡面)

//設定狀態列顏色 - (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } } - (void)viewDidLoad { [super viewDidLoad]; [self setStatusBarBackgroundColor:[UIColor redColor]]; self.view.backgroundColor = [UIColor yellowColor]; }

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

//設定字型顏色 - (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;//白色 } //設定狀態列顏色 - (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } } //!!!重點在viewWillAppear方法裡呼叫下面兩個方法 -(void)viewWillAppear:(BOOL)animated{ [self preferredStatusBarStyle]; [self setStatusBarBackgroundColor:[UIColor redColor]]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; }

//設定狀態列顏色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}
-(void)viewWillAppear:(BOOL)animated{
 
    [self setStatusBarBackgroundColor:[UIColor redColor]];
     [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
}
--->!!!同時別忘了在info plist裡面將View controller-based status bar appearance設定成NO,(預設是YES)
現在基本的裝置都適配ios7以上裝置,預設的狀態列字型顏色是黑色
[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleDefault;
現在基本的裝置都適配ios7以上裝置,預設的狀態列字型顏色是黑色
[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleLightContent;