想定制导航栏吗?从iOS5开始你就可以改变导航栏的背景图片、tintcolor或者标题文本。
这里我们将介绍如何在Xcode中定制导航栏。
 
从这里你可以下载到本文用到的按钮: Buttons.zip
首先需要获取UINavigationController引用。你可以在创建UINavigationController时定制导航栏,也可以在ViewController加载到UINavigationController时定制导航栏。
打开要引用UINavigationController的类,在viewDidLoad方法中定制导航栏的背景图片,如下列代码所示:
 
UIImage  *navbarPortrait =  [[UIImagep_w_picpathNamed :@ "navbar_44.png" ]                               
 resizableImageWithCapInsets :UIEdgeInsetsMake ( 0 ,  0 ,  0 ,  0 )] ;     
UIImage  *navbarLandscape  = [[UIImage p_w_picpathNamed :@ "navbar_32.png" ]                                
resizableImageWithCapInsets :UIEdgeInsetsMake ( 0 ,  0 ,  0 ,  0 )] ;      
// Setbackgroundp_w_picpath for all navigationcontrollers     
[[UINavigationBar appearance ] setBackgroundImage :navbarPortrait                                        
forBarMetrics :UIBarMetricsDefault ] ;    
[[UINavigationBarappearance ] setBackgroundImage :navbarLandscape                                       
 forBarMetrics :UIBarMetricsLandscapePhone ] ;
 
 
导航栏的背景图片必须是下列高度:
竖屏:44px(retina 屏则为 88px)
横屏:32px (retina 屏则为 64px)
 
BarButton(包括backButton)的背景图片用backgroundp_w_picpath属性设置。BarButton的背景图片必须是下列高度:
竖屏:30px(retina 屏则为 60px)
横屏:24px(retina 屏则为 48px)
 
如果标题文本过长(超过按钮图片的宽度)图片将被缩放。
   
// Setbackgroundp_w_picpath for all buttons    
UIImage  *buttonPortait =  [[UIImagep_w_picpathNamed : @"button_30.png" ]                               
resizableImageWithCapInsets :UIEdgeInsetsMake ( 0505 )];     
UIImage  *buttonLandscape  = [[UIImage p_w_picpathNamed : @"button_24.png" ]                                 
resizableImageWithCapInsets :UIEdgeInsetsMake ( 0505 )];     
[[UIBarButtonItem appearance ] setBackgroundImage :buttonPortait                                             
forState :UIControlStateNormal                                            
barMetrics :UIBarMetricsDefault ];    
[[UIBarButtonItemappearance ] setBackgroundImage :buttonLandscape                                             
forState :UIControlStateNormal                                           
barMetrics :UIBarMetricsLandscapePhone ];    
// Setbackgroundp_w_picpath for all backbuttons    
UIImage  *backButtonPortait =  [[UIImagep_w_picpathNamed : @"back_button_30.png" ]                                  
 resizableImageWithCapInsets :UIEdgeInsetsMake ( 01305 )];     
UIImage  *backButtonLandscape  = [[UIImage p_w_picpathNamed : @"back_button_24.png" ]                                    
 resizableImageWithCapInsets :UIEdgeInsetsMake ( 01004 )];    
[[UIBarButtonItem appearance ]setBackButtonBackgroundImage :backButtonPortait                                                      
 forState :UIControlStateNormal                                                     
barMetrics :UIBarMetricsDefault ];    
[[UIBarButtonItemappearance ]setBackButtonBackgroundImage :backButtonLandscape                                                      
forState :UIControlStateNormal                                                     
barMetrics :UIBarMetricsLandscapePhone ];
 
还可以通过titleTextAttriutes属性定制UIBarButton。setTitleTextAttributes:方法用一个NSDictionary参数指定所需选项:
UITextAttributeTextColor,定制文字颜色。
UITextAttributeTextShadowColor,定制文字的阴影色。
UITextAttributeTextShadowOffset,定制阴影的偏移位置。
UITextAttributeFont,定制文本字体。
 
// Settitletext attributes    
NSMutableDictionary  *attributes =  [[ NSMutableDictionary alloc ] init ];
    
[attributessetValue :[UIColor blackColor ] forKey :UITextAttributeTextColor ];    
[attributessetValue :[UIColor whiteColor ] forKey :UITextAttributeTextShadowColor ];    
[attributessetValue :[ NSValue valueWithUIOffset :UIOffsetMake ( 01 )] forKey :UITextAttributeTextShadowOffset ];    
[attributessetValue :[UIFont fontWithName : @"Verdana"size : 0.0 ] forKey :UITextAttributeFont ];      
[[UIBarButtonItemappearance ] setTitleTextAttributes :attributes forState :UIControlStateNormal ];    
[attributesrelease ];