Reputation: 981
I have some view controllers with a toolbar at top and look like this
How I can fill the status bar background to match the toolbar background so its match the new iOS 7 style?
Upvotes: 1
Views: 6176
Reputation: 6529
you need to add a subview in app delegate and change the color to your liking. here is a sample of the code that sits in applicationdidfinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
//change this to match your navigation bar or view color or tool bar
//You can also use addStatusBar.backgroundColor = [UIColor BlueColor]; or any other color
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.window.rootViewController.view addSubview:addStatusBar];
}
return YES;
}
take a look at the comments. you can use any type of color in addStatusBar.backGroundColor
to match the color you need. note that the color with red here is just producing a Black background, change that to whatever you need. for dark grey replace that with the following code:
addStatusBar.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1];
edit:
for changing the status bar color in individual views you simply plug in the following code ( which will access the view you want to change) in ViewDidLoad
method right after [super viewDidLoad];
that should change the color of the status bar in just that view you place the code in.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.view addSubview:addStatusBar];
edit1:
in case of trying to add a subview on top of the nag bar in navigation controller, you have to twice the position of the subview, something like the following:
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, -20, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:127.0/255. green:0.0/255. blue:127.0/255. alpha:1];
[self.view addSubview:addStatusBar];
[self.navigationController.navigationBar addSubview:addStatusBar];
and also you need to add your subview as yet the subview of the navigation controllers' navigation bar. i set the background color of the subview to purple but you can change that.
Upvotes: 8