2010-12-03 47 views
1

如何完成UINavigationBar自定義?它使用子類還是類?在福克斯新聞iPhone應用程序中自定義UINavigationBar

Fox news iphone app

我感興趣的兩個方面:

  1. 添加圖像到的NavBar(如福克斯新聞標誌)
  2. 自定義後退按鈕 「顯示」。 (後退按鈕通常需要在堆棧中的前一視圖的標題,但在前面的視圖中沒有標題。)

預先感謝任何幫助

回答

7

對於福克斯新聞應用程序,它看起來像他們舉st設置導航欄的色調顏色。至於福克斯新聞標誌,它可能只是導航欄標題視圖上的圖像視圖。這個代碼進入一個視圖控制器的viewDidLoad方法:

[self.navigationController.navigationBar setTintColor:/* Custom color here */]; 

UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; 
[self.navigationItem setTitleView:logoView]; 
[logoView release]; 

要定製你需要把這個在以前視圖控制器的viewDidLoad方法的返回按鈕(即該按鈕導致回一個):

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Shows" 
    style:UIBarButtonItemStyleBordered target:nil action:nil]; 
[self.navigationItem setBackBarButtonItem:backButton]; 
[backButton release]; 

如果你想使用一個完全自定義的背景圖片爲您的應用程序的導航欄上,你需要創建一個自定義UINavigationBar類別及其drawRect:方法中繪製圖像。類似這樣的:

@implementation UINavigationBar (UINavigationBarBackgroundImage) 

- (void)drawRect:(CGRect)rect 
{ 
    [[UIImage imageNamed:@"navigation-bar"] drawInRect:rect]; 

    // Optionally you can set the tintColor here to go with the background 
} 

@end 
0

添加圖像導航欄使用這個代碼

- (void)drawRect:(CGRect)rect { 

UIColor *color = [UIColor blackColor]; //this use to set button color in FoxNew Site //button color 
UIImage *img = [UIImage imageNamed: @"ImageName"]; 
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
self.tintColor = color; 

} 

創建類別UINavigationBar的

@implementation UINavigationBar (UINavigationBarCategory) 
    - (void)drawRect:(CGRect)rect { 
    } 

@end 
+0

我試過了,但是當navbar動畫時(當新視圖被按下或彈出時),圖像不會動畫。 – Shameem 2010-12-03 07:53:04

相關問題