Wrong background image for navigation bar - ios

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];
}

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?

Can't set my navigation bar background image properly

I have a view, and inside this view I have a table view.
I want to change this table view navigation bar image to some image.
So I added a navigation bar object in the interface builder and put it on the table view > I control dragged from this object to the table view controller > then in the viewDidLoad I adder the line: [self.navBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
With navBar is the outlet to the navigation bar.
Why this does not work?
tnx
Ideally, Navigation Bar should display the background image with the method you used until and unless the project resources contain the image and Navigation Bar is referenced properly.
Anyways, you can embed the View in Navigation Controller
1. Select ViewController in Storyboard
2. Go to Editor -> Embed in -> Navigation Controller
and use the following code in viewDidAppear-
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
Try Adding the below code in AppDelegate
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];

UIModalViewController StatusBar background color

I set an UINavigationBar background tint by appearance using macro color:
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x6DBEE8)];
And in whole navigation based application but in my UIModalViewController does not work:
In my plist i have : View controller-based status bar appearance : YES
and globally i set appearance : [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Here it what it's look like:
That is because in iOS7, the height of UINavigationBar is increased (64 points) when it is contained in a UINavigationController. With the status bar being transparent,when you are presenting a view controller modally, its not in the UINavigationController so the height is normal (44 points) and thus the map view is behind the status bar. You need to handle this in your modal view controller. You can:
Hide the status bar altogether (works but might not be preferable in
every situation)
Put a view behind the navigation bar and where the status bar is with
the same background color.
Change the color of the view controller's view itself to the desired color and
offset the y position of your map view to accommodate the status
bar's height.

UINavigationBar appearance setBackgroundImage Hides Status Bar

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];
}

Resources