Can't set my navigation bar background image - ios

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?

Related

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

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

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
{
}

Popup button not visible when the splitview is in portrait orientation

I've added another Storyboard for the iPad to my app, trying to make my first universal app.
I'm starting up with a UISplitViewController where I have a menu on the left and the detail controller on the right.
I've customized the navigation bar in the detail view and now the popup button (the one that should show the menu when the iPad is in portrait orientation) does not show.
This is the code I'm using to customize the navigation bar in viewDidLoad:
UIImage *navBarImg = [UIImage imageNamed:#"someimage"];
[self.navigationController.navigationBar setBackgroundImage:navBarImg forBarMetrics:UIBarMetricsDefault];
UIImage *logo = [UIImage imageNamed:#"logo"];
self.navBar.titleView = [[UIImageView alloc] initWithImage:logo];
Then, in the delegate method for the split view "willHideViewController", I'm trying to customize the Menu button:
UIImage *menuBtn = [UIImage imageNamed:#"menu"];
menuBtn = [menuBtn resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 4.5, 0.0, 4.5)];
barButtonItem.image = menuBtn;
barButtonItem.title = NSLocalizedString(#"Menu", nil);
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
I've tried to remove the navigation bar and menu button customizations and the button shows up.
What's wrong with my code?
EDIT:
Following Nazir's advice, I've added the following code to my viewDidLoad method:
UIImage *menuBtn = [UIImage imageNamed:#"menu"];
menuBtn = [menuBtn resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 4.5, 0.0, 4.5)];
[self.navigationItem.leftBarButtonItem setBackgroundImage:menuBtn forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.leftBarButtonItem setTitle:#""];
I forgot that button didn't need a title, but if I don't set it, the button will not appear.
Now the problem is that I'm messing up something with the Storyboard.
At the app start up I want to see the menu on the left (MasterViewController) and a tableview with some data on the right (DetailViewController).
So the relationship segue connecting the Split View Controller to the Detail View Controller is of type "detail view controller".
Now in the menu, beside all the other options, there's the first option used to go back to the first detail view controller (a kind of "Home" option), so I've also connected the Master View Controller with the Detail View Controller using a "Replace" segue.
So what happens now is that the menu button appears when the orientation is portrait, but if I tap on the "home" option of the menu, the button disappears.
That button is supposed to be visible on all the different detail view controllers that I'm going to create, so it's very important.
I hope I made myself clear enough.
If i got what you want to do - then add background to topBatButton so you could do as:
Do it in DetailViewController.m
UIImage *menuBtn = [UIImage imageNamed:#"someimage"];
Setting the image to Bar item:
[self.navigationItem.leftBarButtonItem setBackgroundImage:menuBtn forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Setting the title to Bar item:
[self.navigationItem.leftBarButtonItem setTitle:#"sometitle"];
You cannot have a titleView AND a leftBarButton item (if I read the docs right)
As per the docs:
Titleview: "If you set this property to a custom title, it is displayed instead of the title. This property is ignored if leftBarButtonItem is not nil."
SO I assume the inverse is true too

Resources