UINavigationBar with solid color or image background

How about a nice navigation bar without default iPhone gradient or with a beautiful background image? Lets do it! The thing we’re going to do in both sub-solutions is to override UINavigationBar drawRect function with a help of category. As you may or may not know categories helps us to add additional functionality to an existing classes even if we can’t access their source directly. For more information read Extending Classes in Objective-C With Categories.

UINavigationBar category

First, as I mentioned before, lets override drawRect method with our own. I usually drop this code at the bottom of my application delegate, but you may want to be more organized than that.

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
}
@end

At this point if you run your application (just don’t forget to actually have UINavigationController with visible navigation bar in it) navigation bar will look like.. em.. black rectangle. It means we successfully taken over the control of it’s drawing method and can move further.

Solid color background

So I’ve heard you hate gradients? Since, we’ve already have UINavigationBar in our hands, we can draw a rectangle over it. A nice gray rectangle! Enough of gray already? Okay, red rectangle it will be..

- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
}

And just look at our results! Amazing!

But aren’t we forgetting something? We’re not done here yet. Remember that iPhone uses UINavigationBar property tintColor to style buttons in navigation bar, so unless we want blue buttons in red background (just think about poor eyes of your application users!) we need to alter our code a little bit.

- (void)drawRect:(CGRect)rect {
	UIColor *color = [UIColor redColor];
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
	CGContextFillRect(context, rect);
	self.tintColor = color;
}

Background image

Using image for UINavigationBar background is even more simple! In this example I’ll use 320×44 custom image which also displays my drawing skills.

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

Some important things to have in mind: decide what color of buttons suits your background best, keep in mind iPhone dimensions while drawing your background image and don’t forget about landscape mode if your application is going to support it.

84 responses

  1. Posted May 11, 2013 at 11:11 | Permalink

    You really make it appear so easy along with your presentation but I find this matter to be really something that I believe I’d by no means understand. It kind of feels too complex and very large for me. I’m taking a look forward for your subsequent publish, I will attempt to get the grasp of it!

  2. Posted May 20, 2013 at 04:31 | Permalink

    It’s going to be ending of mine day, but before end I am reading this wonderful piece of writing to improve my experience.

  3. Posted May 28, 2013 at 13:01 | Permalink

    naturally like your web site but you need to test the spelling on quite a few of your posts. Several of them are rife with spelling issues and I to find it very troublesome to inform the truth nevertheless I’ll certainly come again again.

  4. Posted July 5, 2013 at 01:08 | Permalink

    A false alarm Cronic Sinus Infection scare with a 2 year old, and 12
    to 15 months might help.

  5. Posted July 9, 2013 at 02:46 | Permalink

    Liver Cirrhosis Internal Bleeding is usually a childhood disease,
    the introduction of the chickenpox vaccine can be offset by treating
    adults with a ‘shingles’ vaccine.

  6. Posted January 17, 2014 at 02:35 | Permalink

    I like the valuable information you supply in your articles.
    I will bookmark your weblog and take a look at once more right here frequently.
    I am relatively certain I’ll learn many new stuff right right here!
    Good luck for the next!

  7. Posted February 5, 2014 at 04:54 | Permalink

    Neither might a large number of Americans work for the low wages paid.
    If you find yourself designing a vacation, among the
    most important considerations is possibly the budget. Are really the local settlers
    thrilled to see foreigners? You simply cannot choose Philippine Boracay’s processed beach sand and
    also under accommodation of an expensive hotel if you
    are really in an extremely tight budget. Our clubs also offer wellness clubs, spa and additionally good dining restaurants
    in picturesque setup.

    My page … best honeymoon all Inclusive

  8. Posted July 3, 2014 at 06:09 | Permalink

    Hey Ι know tɦis is off topic but ӏ was wondering іf yoou knew oof any wiԀgets I cߋkulԀ adɗ to myy
    bloɡ that automatically tweet mƴ newest twitter updɑtes.
    I’ve been lookng ffor a plug-in like this for quyite some time and was ɦoping maybe you wouldd hɑve some experience ԝith something like this.
    Please let me know if you rrun int anything. I truly eʝjoy reading yoir blog and I look forward to your new updates.

  9. Posted July 15, 2014 at 14:03 | Permalink

    Howdy, I uncovered your web site through Bing when seeking a similar subject matter, your internet site came right up, it appears wonderful. I put into my favorites|combined with favorites.

  10. Posted August 4, 2014 at 03:05 | Permalink

    naturally like your web site but you have to take a look at the
    spelling on several of your posts. A number of them are rife with spelling problems
    and I to find it very troublesome to inform the reality then again I will
    definitely come back again.

  11. Posted August 29, 2014 at 22:30 | Permalink

    I’ve been browsing on-line greater than 3 hours lately, yet I by no means discovered any interesting
    article like yours. It is pretty value sufficient for me.
    In my view, if all web owners and bloggers made good content as you did, the web will probably be a lot more useful
    than ever before.

Leave a Reply