How to customize the NavBar in iOS


The NavBar or the navigation bar is the top side view that appears by default over our main view if we have a navigation controller presenting the corresponding view controller.
Its dimensions for iPhone and iPod touch are 320 x 44, so if we want to set custom background images for that, we need 2 images: one normal of 320 x 44 pixels, and the other one for Retina Display, of 640 x 88 pixels.
iOS provides us 4 default options for customising the NavBar, and we do that by writing the following line of code:
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

The “barStyle” property can be set to one of 4 values, which are:
UIBarStyleDefault,
UIBarStyleBlack,
UIBarStyleBlackOpaque,
UIBarStyleBlackTranslucent.

Starting with iOS 5, it is very easy to set a background image for the NavBar in this way:
UIImage *navBarImage = [UIImage imageNamed:@”nav-bar-image”];

[[UINavigationBar appearance] setBackgroundImage:navBarImage forBarMetrics:UIBarMetricsDefault];

Leave a comment