iOS5:定製導航欄

原文:http://www.altinkonline.nl/tutorials/xcode/uinavigationbar/customizing-background-tintcolor-and-selected-image/php

 

想定製導航欄嗎?從iOS5開始你就能夠改變導航欄的背景圖片、tintcolor或者標題文本。xcode

這裏咱們將介紹如何在Xcode中定製導航欄。app

 

從這裏你能夠下載到本文用到的按鈕: Buttons.zip字體

首先須要獲取UINavigationController引用。你能夠在建立UINavigationController時定製導航欄,也能夠在ViewController加載到UINavigationController時定製導航欄。ui

打開要引用UINavigationController的類,在viewDidLoad方法中定製導航欄的背景圖片,以下列代碼所示:spa

 

UIImage *navbarPortrait= [[UIImageimageNamed:@"navbar_44.png"]                               
.net

 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];     
code

UIImage *navbarLandscape =[[UIImage imageNamed:@"navbar_32.png"]                                
orm

resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];      圖片

// Setbackgroundimage for all navigationcontrollers     

[[UINavigationBar appearance] setBackgroundImage:navbarPortrait                                        

forBarMetrics:UIBarMetricsDefault];    

[[UINavigationBarappearance] setBackgroundImage:navbarLandscape                                       

 forBarMetrics:UIBarMetricsLandscapePhone];

 

 

導航欄的背景圖片必須是下列高度:

豎屏:44px(retina 屏則爲 88px)
橫屏:32px (retina 屏則爲 64px)

 

BarButton(包括backButton)的背景圖片用backgroundimage屬性設置。BarButton的背景圖片必須是下列高度:

豎屏:30px(retina 屏則爲 60px)
橫屏:24px(retina 屏則爲 48px)

 

若是標題文本過長(超過按鈕圖片的寬度)圖片將被縮放。

   

// Setbackgroundimage for all buttons    

UIImage *buttonPortait= [[UIImageimageNamed:@"button_30.png"]                               

resizableImageWithCapInsets:UIEdgeInsetsMake(0505)];     

UIImage *buttonLandscape =[[UIImage imageNamed:@"button_24.png"]                                 

resizableImageWithCapInsets:UIEdgeInsetsMake(0505)];     

[[UIBarButtonItem appearance] setBackgroundImage:buttonPortait                                             

forState:UIControlStateNormal                                            

barMetrics:UIBarMetricsDefault];    

[[UIBarButtonItemappearance] setBackgroundImage:buttonLandscape                                             

forState:UIControlStateNormal                                           

barMetrics:UIBarMetricsLandscapePhone];    

// Setbackgroundimage for all backbuttons    

UIImage *backButtonPortait= [[UIImageimageNamed:@"back_button_30.png"]                                  

 resizableImageWithCapInsets:UIEdgeInsetsMake(01305)];     

UIImage *backButtonLandscape =[[UIImage imageNamed:@"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];

 

完。

相關文章
相關標籤/搜索