Add Custom Background Image to UINavigationBar in iOS 5

From iOS 5, Apple has officially supported adding custom background image to theUINavigationBar with setBackgroundImage:forBarMetrics:, making such task easier than ever before.

Setting the UINavigationBar to a custom background image can really dramatically improve your app. With the appropriate image, a custom UINavigationBar makes the overall feeling of the app much better.

1. Set a universal background image for navigation bars of all views

Custom UINavigationBar

Custom UINavigationBar
(Image by App Design Vault)

  1. Find an appropriate custom background image for the UINavigationBar (Great if you have a Retina Display version.) Download the sample image of this demo if you wish.
  2. Drag and drop the images into your project. Remember to check the option “Copy items into destination group’s folder (if needed)”.
  3. Goto AppDelegate.m and paste the code under
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.
    Remember to change “menubar.png” to the file name of your image. (as highlighted in code)
AppDelegate.m
1
2
3
4
5
6
7
//Set the status bar to black color.
[[ UIApplication sharedApplication ] setStatusBarStyle : UIStatusBarStyleBlackOpaque animated : NO ];
 
//Change @"menubar.png" to the file name of your image.
UIImage *navBar = [ UIImage imageNamed : @"menubar.png" ];
 
[[ UINavigationBar appearance ] setBackgroundImage :navBar forBarMetrics : UIBarMetricsDefault ];

Through this, you should have successfully add your custom image to everyUINavigationBar.

2. Set the custom background image for navigation bar of a specific view

Sometimes, instead of having a universal navigation bar, you might need to set the custom background image for navigation bar of a specific view. You should do this through following steps.

  1. Find the appropriate image and add it to your project.
  2. Subclass the specific UIViewController for which you want to have a different navigation bar.
    For example, I want to designate a certain view in the storyboard to have a different navigation bar. So I created a class called SpecialViewController and hooked up the view to this class.
  3. Goto AppDelegate.m and paste the code under
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
AppDelegate.m
1
2
3
4
5
6
// Rename "specialNavBar.png" to the file name of your specific navigation bar background image.
UIImage *special = [ UIImage imageNamed : @"specialNavBar.png" ];
 
// Rename "SpecialViewController" to the class name that you want to have a different navigation bar for.
[[ UINavigationBar appearanceWhenContainedIn :[SpecialViewController class ], nil ] setBackgroundImage :special
forBarMetrics: UIBarMetricsDefault ];

Remember to change specialNavBar.png to the file name of your special navigation bar background image and change SpecialViewController to the class name that you want to have a different navigation bar for. (as highlighted in code)

From:http://www.lwxted.com/blog/2012/add-custom-background-image-uinavigationbar-ios-5/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值