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.

96 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.

  12. Posted September 22, 2014 at 19:09 | Permalink

    Wow, that’s what I was searching for, what a stuff! present here at
    this website, thanks admin of this site.

    Have a look at my web page :: Gabriel I. Twedt

  13. Posted September 24, 2014 at 21:13 | Permalink

    This piece of writing will assist the internet people for setting up new
    blog or even a weblog from start to end.

    Feel free to surf to my web site; International Maritime Consultants

  14. Posted September 27, 2014 at 11:09 | Permalink

    Hey I am so grateful I found your site, I really found you
    by mistake, while I was looking on Aol for something else, Anyways I am here now and
    would just like to say thank you for a incredible post and a all round enjoyable blog (I also love
    the theme/design), I don’t have time to browse
    it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep
    up the awesome jo.

  15. Posted September 27, 2014 at 11:48 | Permalink

    fantastic points altogether, you simply won a new reader. What may
    you suggest about your post that you made some days ago?
    Any positive?

  16. Posted September 28, 2014 at 23:48 | Permalink

    Awesome post.

  17. Posted September 29, 2014 at 15:43 | Permalink

    Hmm it appears like your blog ate my first comment (it
    was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I’m still new to the whole thing.

    Do you have any tips and hints for rookie blog
    writers? I’d definitely appreciate it.

  18. Posted October 7, 2014 at 01:48 | Permalink

    Write more, thats all I have to say. Literally,
    it seems as though you relied on the video to make your point.
    You definitely know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something informative to read?

  19. Posted October 7, 2014 at 09:11 | Permalink

    An impressive share! I have just forwarded this onto a coworker who was conducting a little homework on this.
    And he actually ordered me dinner due to the fact that I found it for him…
    lol. So let me reword this…. Thanks for the meal!! But yeah, thanx for spending time to talk about this subject here
    on your internet site.

  20. Posted October 8, 2014 at 17:30 | Permalink

    Spot on with this write-up, I honestly feel this amazing site
    needs far more attention. I’ll probably be returning to read through more, thanks for the information!

  21. Posted October 11, 2014 at 02:59 | Permalink

    There is definately a great deal to know about this
    topic. I really like all the points you made.

  22. Posted October 12, 2014 at 14:15 | Permalink

    There are determining factors that you will have to think about, including the repair costs,
    the price of the vehicle in question and the existing
    damage. I’m not sure how hard it is to find “pet friendly” dwellings in Los Angeles, but in my neighborhood there are more dogs than people and that must mean something.
    The one-minute video features a first look at official footage from
    the movie and only a brief glimpse of Katniss, as she walks through one of the war-torn districts.
    Dusseldorf court in Germany has recently asked
    Samsung to stop selling the Galaxy Tab 10. The display screen is provided with Super IPS + LCD Capacitive touch
    screen along with Corning Gorilla Glass protection. Sony’s statement mentions “maintenance,” not another hack, though.
    The power of this new DS being on par with the Gamecube is what I’m more interested in though, because if that’s true it’ll make for one damn good hand held console and great competition for the PSP.
    Over the 260 million worldwide players, only 40 million play in a club.

    Seraphs, unlike Banshees, are not complete pushovers.
    Take this information only as a rumor at this point.
    It’s ridiculous, crazy and completely irrelevant to anything.
    When we were children, almost all of us often wished to become a beautiful
    princess in one fairy tale and get married to a handsome prince.

    To make matters even better, it’s free to play as well.
    You can also use the Replication Monitor (Replmon) or Repadmin command-line tool to learn more about replication performance.
    Also, the cloud-based Drivatar learning opponent system
    in Forza Motorsport 5 is a crucial innovation that impacts practically every aspect of the
    game. Documenting the conflict between Palestinians and Israelis is the
    last thing on Emad’s mind. What a wonderful
    government we have, they certainly know what they’re doing.
    Sprinkle mint leaves and other ingredients to garnish the fruit platter.
    There are hundreds of games for you to select from swimming games, shopping games to pool games.
    On Thursday, Digital Spy shared the latest comments made by Donald Sutherland about the film.
    Wii Match couldn’t reside up to the buzz people imagined it could really be considered
    exercise. This is what exactly you will be
    able to do in a dress up game. What they don’t see, is
    him throwing away his count sheet without even looking at it.
    What distinguishes the New Atheism from the atheism of the past is
    the strident, vocal nature of the New Atheism. It’s going to add a whole
    new dimension to the Museum.

  23. Posted October 18, 2014 at 01:38 | Permalink

    Asking questions are actually good thing if you are not understanding anything completely, except this
    article provides good understanding yet.

Leave a Reply