I'm developing an app using iOS 7 and storyboards. I have several view controllers. What I want to do is change the status bar color according to the Navigation bar color. But each viewcontroller has a different color navigation bar. I know I can change the status bar colour by adding a flag to info.plist and code like this:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.window.clipsToBounds = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
self.window.frame = CGRectMake(20, 0,self.window.frame.size.width-20,self.window.frame.size.height);
self.window.bounds = CGRectMake(20, 0, self.window.frame.size.width, self.window.frame.size.height);
} else
{
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
}
But how do I change the color for each ViewController?
Add this method to each viewCOntroller
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault; // or UIStatusBarStyleLightContent
}
In viewDidLoad for each view controller:
[self preferredStatusBarStyle];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
Also in project properties you need to set to YES this option: View controller-based status bar appearance
Related
I'm using SKStoreProductViewController for opening the appstore in my app. it was presenting successfully,but the statusbar in it was not showing, Navbar looks like a 44pixels height. But it won't happens in any iOS 8.3v in any device.
This situation happens only in all iOS 8.4v devices.
In my plist UIViewControllerBasedStatusbarAppearance is set to NO. i tried with Yes also, but no use.
the Red color in status bar is superview navigationbar color.
NOTE: i'm presenting the SKStoreProductViewController from my ParentViewController.
Any help will be greatly appreciated.
I solved this by adding statusBar as UIView Component, Its a workaround to the solution. I also faced similar issue.
SKStoreProductViewController *_apsvc = [[SKStoreProductViewController alloc]init];
_apsvc.delegate = self;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
//change this to match your navigation bar
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
addStatusBar.frame = CGRectMake(0, 0, viewController.view.frame.size.width, 20);
addStatusBar.backgroundColor =[UIColor whiteColor];
}
UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
if (viewController)
{
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:_apsvc animated:YES completion:^()
{
[_apsvc loadProductWithParameters:#{SKStoreProductParameterITunesItemIdentifier : AppID}
completionBlock:^(BOOL result, NSError *error) {
if(error)
{
....
}
}];
[viewController.view addSubview:addStatusBar];
}];
}
}
-(void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
[addStatusBar removeFromSuperview];
if (viewController)
{
[viewController dismissViewControllerAnimated:YES completion:nil];
}
}
im using UIImagepickercontroller to take photo and choose existing photos, when the camera view appears, the top portion on camera was covered by the white status bar. How can i remove the status bar or hide the status bar. Many of them saying its a ios 7 bug, it has been fixed in ios 7.1, but still i'm facing this problem.
This is my code to show the imagepickerController
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = (id)self;
imagePicker.sourceType = (buttonIndex == actionSheet.firstOtherButtonIndex ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary);
[self.viewController presentViewController:imagePicker animated:YES completion:nil];
Also i tried by with below codes to hide statusbar
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
This
-(BOOL) prefersStatusBarHidden {
return YES;
}
And This in Delegate
[UIApplication ShareApplication]SetStatusbarHidden:YES];
Right now i'm working with ios8, and this behaviour happening in all ios version.
Please help me out.
Actually the problem is in my rootviewcontroller i'm creating a Statusbar using UIView and setting its constraints for landscape and portrait programmatically
- (void)setTopBar {
if(topBar == nil && [[UIApplication sharedApplication] keyWindow] != nil) {
topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
topBar.backgroundColor = navController.view.backgroundColor;
topBar.tag = topBarViewTag;
UIView *containerView = [[UIApplication sharedApplication] keyWindow];
[containerView addSubview:topBar];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view(20)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:#{#"view" : topBar}]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[view]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:#{#"view" : topBar}]];
[containerView layoutSubviews];
}
}
so when i commented the above method in ViewwillAppear, status bar where not showing up, so the problem is with my code.
if anyone face the same issue, i suggest you to have a look on your RootViewcontroller, which acts a navigation to all other classes.
i am doing one application.In that i used the below code to stop the override the status bar in my view
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.window.clipsToBounds = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
self.window.frame = CGRectMake(20, 20,self.window.frame.size.width-20,self.window.frame.size.height);
self.window.bounds = CGRectMake(20, 0, self.window.frame.size.width, self.window.frame.size.height);
} else
{
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
}
But if i do this i am getting the black transparent bar at the bottom of the view in simulator.But in device i am getting black color.And it is correctly working in ios6,only problem i ios 7 like below.
I ran your code in a demo with default navigation bar and found same black stuff coming. So made some changes and below is working one -
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
self.window.frame = CGRectMake(0, 0,self.window.frame.size.width,self.window.frame.size.height);
self.window.bounds = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height);
} else
{
self.window.frame = CGRectMake(0,0,self.window.frame.size.width,self.window.frame.size.height);
self.window.bounds = CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height);
}
You could give it a ran and let me know if anything come's up.
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?
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.
I have an app which starts with a root UINavigationController. This controller then pushes and pops various other UIViewControllers - that all works fine.
My app has a custom graphic for the navigationController.navigationBar.backgroundImage. There is one image for the iPhone platform. There are two images for the iPad platform - one for portrait, one for landscape. The iPad is the platform of issue as it rotates, the iPhone platform is portrait only.
I had already written code in -(void)willRotateToInterfaceOrientation: to detect the rotation, and set the navigationBar.backgroundImage to the correct orientation graphic, if the platform is iPad.
In the iPad 5.0 simulator - it works fine; rotating the iPad on screen results in the correct navigationBar graphic regardless of orientation.
On the device - it doesn't work - The graphic appears correctly on iPad device launch in portrait. When I rotate to landscape, the navigationBar changes to a standard grey one. When I rotate back the grey 'sticks'.
I have tried calling setNeedsDisplay - no change.
I have tried setting navigationBar.tintColor = [UIColor clearColor] - no change.
I have checked the filenames of the graphics in the code are identical to the actual filenames - they are.
Code for rotation:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return YES; }
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
UIImage *titleImage = [UIImage imageNamed:#"StandardHeaderIpadLandscape"];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics:UIBarMetricsDefault];
self.navigationController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"StandardBackgroundiPadLandscape.png"]];
[self.navigationController.navigationBar setNeedsDisplay];
} else {
UIImage *titleImage = [UIImage imageNamed:#"StandardHeaderIpadPortrait"];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics:UIBarMetricsDefault];
self.navigationController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"StandardBackgroundiPad.png"]];
[self.navigationController.navigationBar setNeedsDisplay];
}
}
}
Check whether titleImage is nil. I have a feeling that your image paths are not correct, or those images are not correctly copied to iPad.