OS5開始你就可以改變導航欄的背景圖片 導航欄的背景圖片必須是下列高度:44 BarButton 30

想定製導航欄嗎?從iOS5開始你就可以改變導航欄的背景圖片、tintcolor或者標題文本。
這裏我們將介紹如何在Xcode中定製導航欄。
 
從這裏你可以下載到本文用到的按鈕: Buttons.zip
首先需要獲取UINavigationController引用。你可以在創建UINavigationController時定製導航欄,也可以在ViewController加載到UINavigationController時定製導航欄。
打開要引用UINavigationController的類,在viewDidLoad方法中定製導航欄的背景圖片,如下列代碼所示:
 
UIImage *navbarPortrait= [[UIImageimageNamed:@"navbar_44.png"]                               
 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];     
UIImage *navbarLandscape =[[UIImage imageNamed:@"navbar_32.png"]                                
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];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章