UINavigationBar appearance setBackgroundImage Hides Status Bar - ios

UIImage *gradientImage46 = [[UIImage imageNamed:#"navbar.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[UINavigationBar appearance] setBackgroundImage:gradientImage46
forBarMetrics:UIBarMetricsDefault];
I am using this to customize the appearance of my navigation bars in my app. However, this is causing the status bar to be solid black, showing nothing...time, carrier, battery, etc. Is this a bug, or did I implement this incorrect?
I have this running in a tab bar using MainWindow.xib method for interface. The first tab is just a navigation controller with a view controller inside it. Another tab is a Navigation Controller with a TableView Controller inside. If I go from one tab to the table view, and then back, the status bar appears.
The navbar.png is 320 x 44 pixels.

I also have this problem bringing my app from iOS 6 to iOS7, I solved by changing the code in this way:
instead
[UINavigationBar appearance] setBackgroundImage:gradientImage46
forBarMetrics:UIBarMetricsDefault];
i use
-(void) viewWillAppear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:gradientImage46 forBarMetrics:UIBarMetricsDefault];
}

Related

ios - Set Status Bar Background color

Apps like Facebook, Instagram and YikYak seem to have a navigation bar that's coloured, and when its swiped, it hides however, the status bar is still there with a background colour thats the same as the navigation bar?
Example:
Facebook Navigation Bar Shown
Then user swipes up (on a tableview or something) then the navigation bar hides but the status bar is still there with a coloured background.
How can I implement this?
I have achieved a coloured Navigation bar with a white status bar that hides on swipe with this code:
// Set Navigation Bar Color
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:(252/255.0) green:(103/255.0) blue:(105/255.0) alpha:1.0]];
// Set Status Bar Style
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Now I just need to figure out how to change the status bar's background colour.
I have gotten close with the following code (that I add to my app delegate didFinishLaunchingWithOptions) that adds a coloured view, however it adds the view to the very top of my view controller hierarchy, where as I just want to add the view to my table view controller.
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
view.backgroundColor=[UIColor colorWithRed:(252/255.0) green:(103/255.0) blue:(105/255.0) alpha:1.0];
[self.window.rootViewController.view addSubview:view];
Does anyone know how to ether add this view to just one of my view controllers, or a better way to do this?

Can't set my navigation bar background image

I want to change my table view navigation bar background image to some png, i created the nav bar png with the exact sizes of a default nav bar (I checked in interface builder).
This is my code (in my table view controller class, viewDidLoad method):
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"nav.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Shouldn't this work..?
this is my table view settings in the interface builder if this help:
the top one is what i suppose to get and the bottom one is what im getting:
thanks!
UIImageView *backImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"nav.png"]];
[self.navigationController.navigationBar addSubview:backImgView];
Try to add Image in custom view
Why not use the existing nav bar? Don t think you need to create a new one... This code is working for me :
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
Your TableViewController must be "embadded" in a navigation controller a.k.a connected to other ViewControllers as push segue... Then you get your nav bar automatically instead of creating one... I mean you create a nav bar and never add it to the view isnt it?

Wrong background image for navigation bar

I ran into this issue in iOS 8 where the wrong background image for the navigation bar is displayed in any of these situations:
the view controller-based status bar appearance is set to YES (default) and the navigation controller is presented modally.
the view controller-based status bar appearance is set to NO and the status bar is initially hidden. In this case, the navigation controller does not need to be presented modally to display the wrong image.
To isolate the problem where the view controller-based status bar appearance is set to YES (default) and the navigation controller is presented modally I created a test project from scratch following these steps:
Create a new project with the "Master-Detail Application" template.
Open Main.storyboard and add a Navigation Controller to it. Remove its root view controller and connect the Master View Controller with a modal segue. Then connect the Detail View Controller as its root view controller. You should end up with something like this:
Customise the navigation bar background via the appearance proxy. Use two different images for portrait (UIBarMetricsDefault) and landscape (UIBarMetricsCompact). I used a category on UIImage to create the images from solid colors.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor blueColor]] forBarMetrics:UIBarMetricsCompact];
return YES;
}
Run the app. The Master view controller displays the right navigation bar background images for portrait and landscape orientations:
Now present the Detail view controller modally. To do so, tap on the "Add" button and then select the newly created row. The Detail view controller displays the right navigation bar background image.
Rotate the interface. The Detail view controller will not change the navigation bar background image (wrong):
Rotate the interface back to its original orientation. The Detail view controller will change the navigation bar background image (wrong):
Has anyone else struggled with this?
Yes,I have been struggling with this, after I add "View controller-based status bar appearance" in target's plist file, it is working again on iOS 8, remember to set it to NO.
In MasterViewController, add this to avoid wrong background image when detailViewController is dismissed. It basically set the same appearance again.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor blueColor]] forBarMetrics:UIBarMetricsCompact];
}

Issued changing the navigationbar image?

Currently, I am able to change the tint of the my UINavigationBar by
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:74/255.0f green:74/255.0f blue:74/255.0f alpha:1.0f];
However when I try to implement the following code to change the image of the UINavigationBar all together, I see no results..
UIImage *navImageBackground = [[UIImage imageNamed:#"texturedNav"] //This being my .png image
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navImageBackground
forBarMetrics:UIBarMetricsDefault];
What am I doing wrong here?
You're probably calling the appearance delegate after the navigation bar is already on screen so your request is being 'ignored'. Or, at least, not causing a UI update.
Set the appearance before the navigation bar is shown. Or, force a UI refresh by hiding and showing the navigation bar (without animation).

Changing navigation bar image - not working

I'd like to change the background image (or color) of the navigation bar, but for some reason it's not working.
I've searched around, and found that on iOS 5.0+ (I'm running 6.1) this should work:
[self.navigationController.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
Now none of those work. Also tried with:
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
Now I'm thinking that I'm assigning it to the wrong object, but this works:
UIImageView *titleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"scr1_title"]];
self.navigationItem.titleView = titleImage;
Use this code in your view controller to change the navigation bar background image.
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
UIImage * navBarImage = [UIImage imageNamed:#"image.png"] ;
[[UINavigationBar appearance]setBackgroundImage: navBarImage forBarMetrics:UIBarMetricsDefault];
}
You can set the tint color on the navigation bar of the navigation controller. Make sure you select the correct on as shown in the screenshot below.
Select your storyboard in the file browser.
Select the navigation controller in the storyboard.
In the Document Outline (second column) select the navigation bar of the navigation controller.
In the Utilities view (column on the right) adjust the tint color.
Click on the root view controller to the right on the navigation controller. It will pick up the new color after you click on it.
To set the image..
Write this code
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationBar"] forBarMetrics:UIBarMetricsDefault];
inside
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}

Resources