Can't set my navigation bar background image properly - ios

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

Related

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

Custom UINavigationBar Background Working on all Navigation Controllers Except One

I have a series of view controllers connected by a root navigation controller. All of the view controllers are table views, except one which is a view controller.
I have a custom navigationBar image which I've applied in the App Delegate and it works great everywhere, except this one view controller. With this, when the segue happens (Push), it almost super imposes the custom background with another image and the result is a darker uinavigationBar.
My code in the App Delegate is:
UIImage *nav = [UIImage imageNamed:#"Purple.png"];
[UINavigationBar appearance] setBackgroundImage:nav forBarMetrics:UIBarMetricsDefault];
Does anyone have any ideas for what I can do to ensure this view controller maintains the correct custom UINavigationBar?
In the viewDidLoad of this view, I've even tried to set this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Purple.png"] forBarMetrics:UIBarMetricsDefault];
I also tried this in the viewWillAppear but nothing - still that darker image.
Any thoughts would be greatly appreciated!
you wrote this wrong
UIImage *nav = [UIImage imageNamed:"#Purple.png#];
replace that with this
UIImage *nav = [UIImage imageNamed:#"Purple.png"];

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

Missing drop shadow under UINavigationBar in combination with UITableView

When I try to add a custom background to a navigation bar, their is no drop shadow on top of the table view. In the subviews everything is fine.
Here is my code I use.
// Create resizable images
UIImage *gradientImage44 = [[UIImage imageNamed:#"navbar"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];
// Customize NavBar Shadow
[[UINavigationBar appearance] setShadowImage:nil];
Not existing drop shadow
Existing drop shadow
Update:
Found this answer, but does not work. I have done it exactly in the same way :-(.
I fixed it, by reimplementing the the navigation controller. The view hierarchy looked like this.
UIView
+- Navigation View
+- Table View
I changed my UIView to an table view and added this to the UINavigation Controller. That fixed the missing shadow issue.
replace
[[UINavigationBar appearance] setShadowImage:nil];
with
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"SomeShadowImage"]];
And it's for iOS 6.0 and later.
I followed #madcat's answer. In my case the UITableView or UITableViewController (embedded in a Container View) had to be above the Navigation Bar (dragged onto view) in the hierarchy. Like that the Navigation Bar was drawn with the shadow after/over the UITableView/Container

Resources