Adding image to iPhone navigation bar - iphone

I want to replace the current nav bar with a custom image. How my code is structured is that a tab bar controller controls a bunch of navigation controllers which contain views (tab bar controller -> nav controller -> view). I tried using this code in my app delegate
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor];
UIImage *img = [UIImage imageNamed: #"nav.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
#end
But it did not work. Any ideas why? Should I have placed it somewhere else? Thanks!

if you are using iOS 5, you can use setBackgroundImage: like this where ever you initialized the navigation controller (aNavigationController in this example):
[aNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];

I would not use a category. I would subclass UINavigationBar instead. See this: https://stackoverflow.com/a/6959354/472344
If you are targeting iOS 5 or up, use #BJH's solution instead.

Related

Issue with UINavigationBar title alignment

I have an issue changing the font for the Title in my navigation controller.
Here is my code (from my subclass of UINavigationController):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSDictionary * attributes = #{UITextAttributeTextColor :[UIColor whiteColor],
UITextAttributeFont :[UIFont fontWithName:#"Blanch" size:50],
};
self.navigationBar.titleTextAttributes = attributes;
CGFloat verticalOffset = -8;
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed:NAVBAR_IMAGE];
[self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
This is what it looks like:
If I comment out the line setting the font this is what it looks like (as expected with the vertical offset):
The only reason the vertical offset is there is that I thought it might have solved the problem. I don't need it and it is commented out now, but it does NOT change the output - it still looks the same just lower on the NavBar.
Any ideas how to solve this? I think it may be that the custom font has a lot of line-spacing
which I guess could be the problem.
I have been trying to find a way to change the origin/height/baseline alignment of the title but can't see a way to do any of these.
Any idea how to solve this problem ?
Create a container UIView which is the same width as your label, and height of UINavigationBar (around 46 pixels?). Set clipsToBounds on this container view to YES. Now add your label to this container view, and make the container view the titleView.
Setting Navigationbar image for ios5
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];
}
Setting Navigationbar image for ios6
if([[UINavigationBar appearance] respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];
after than create label in center on your navigation bar center with your title.

iOS Dynamic Change UINavigationBar's Background With Image

I have read this and change UINavigationBar's background:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"top-with-logo.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
you see, I add a image named top-with-logo.png, and I want to change the background view by adding an image named top-without-logo.png. But the code above will change the background forever and i cannont change any more. Do you know how?
Thank you very much.
I'm not sure I understand what you mean, but you can place a Boolean ivar on your app's delegate. then in the function you load top-with-logo.png if it's true/false and load top-without-logo.png if it's the other value. then, when you want to change the logo, just change the Boolean's value and call setNeedsDisplay on the navigationBar.
if you use xib files, you can also subclass UINavigationBar, and instead of using a catagory you override the drawRect function and place the variable as an instance variable as a subclass, I believe it is cleaner, but you can only set that up in Interface builder ( otherwise there's no way to tell a UINavigationController to use a different class for the navigationBar property )
You can use,
UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: #"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];

Custom UINavigationController UINavigationBar

Basically I want a custom UINavigationBar. I don't want it to be "translucent" or anything, like the pictures app.
I basically want to completely remove it, but I still want to be able to add back buttons and such when navigation controllers are pushed, and I want the views (EG: UITableViewController) to be pushed down below it.
Like this:
Any ideas how to achieve this at all?
Thanks
#implementation UINavigationBar (background)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"navigationbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
basically, its not completely see through - its a visual lie. The only way to do it realistically is to override UINavigationBar's drawRect: method, as shown above.
To see through the UINavigationBar, if you choose to have one, just:
self.navigationController.navigationBar.translucent=YES;
You'll have to change the tint/color to match the background if you want it to appear like the image you posted.
At the beginning of your AppDelegate subclass UINavigationBar as below:
#interface CustomNavBar : UINavigationBar
#end
#implementation CustomNavBar
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
and then in the AppDelegate do this magic:
//Set custom NavigationBar
[self.navController setValue:[[CustomNavBar alloc]init] forKeyPath:#"navigationBar"];
//Set tint to match bar.png
self.navController.navigationBar.tintColor = [UIColor colorWithRed:0.93 green:0.43 blue:0 alpha:1];
//Set font for NavigationBar
[self.navController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Comfortaa-Bold" size:20], UITextAttributeFont, nil]];
That should give you a lot more control over UINavigationController look & feel.
Hard to tell, could be the UINavigationBar is there and color matches the UIView background or, there is no UINavigationBar, just a view with custom buttons and UILabel on top. Pick an approach and code it, or ask the question again with more specifics.

Tint all UIBarButtonItems in my App (MFMailComposer, etc)

I created a custom Navbar for my application using a protocoll:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"navbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
this works fine. all toolbars in my application look great now. but i also want a way to tint my uibarbuttonitems in my toolbar. i could set the tint color of every button, but this does not work on pre-defined items like the ones in my mailcomposerview and so on.
is there a way to give all my buttons a tint color? just like i've done that with my navigation bar.
You'll need to set the tintColor for navigationBar property of your navigation controller.
self.tintColor = whatever in your drawRect will do it

iPhone UINavigationController problems (invisible Button, title)

I have two problems with the UINavigationController.
Problem 1
On the initial view I programmatically added a rightBarButtonItem.
When I push the navigationView to the right, everything works right and it appears a back button with the title of the previous view.
When I click the back button, I get back. But then this button is gone. But it's not really gone, it's just invisible, because it can still be clicked.
Problem 2
When I push the viewController on the right, I can't set the title. I tried it before pushing the view with
myViewController.title = #"someTitle";
and I tried it also inside of the viewController with
self.title = #"someTitle";
but nothing works here.
I found the problem:
I was adding a custom background image to the navigationBar. Somehow this image got over the title / button.
So if you ever want to implement a custom background image, don't user this:
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[backgroundImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"headerBg" ofType:#"png"]]];
[navigationController.navigationBar insertSubview:backgroundImage atIndex:0];
[backgroundImage release];
But instead, use this:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"headerBg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Problem 1:
Sounds odd. Don't really have an answer for that :-/
Problem 2:
Can you provide some more code snippets on this, like the whole method bodies where you tried to set the title?