2012-06-02 177 views
6

我繼續讀來改變一個導航欄我只需要在AppDelegate中更新第一種方法來此更改導航欄的顏色(背景色)

self.window.rootViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor]; 

默認的顏色,但它似乎沒有工作,所以我試圖將它設置在firstview的viewDidLoad方法中:

self.parentViewController.navigationController.navigationBar.tintColor = [UIColor whiteColor]; 

這也沒有效果。我該如何改變這一點?

回答

0

嘗試self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
UIViewController類有一個屬性navigationController,它返回導航控制器的嵌入不管有多深,否則返回nil

6

不要使用self.parentViewController,但self

self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; 
1

當iPhone 5的到來,我們必須同時設置設備類型。因此,使用這種

if([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    //iOS 5 new UINavigationBar custom background 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg_ForiPhone5_Imagename.png"] forBarMetrics: UIBarMetricsDefault]; 
} else { 
    [self.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navbg_ForOtherIphone_Imagename.png"]] atIndex:0]; 
} 
+0

我更喜歡在色調上使用UIImage。謝謝。 – Flea

1

在IOS7你可以試試這個:

[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; 

您可以按照下列步驟操作:

我創建了一個新的UINavigationController例如UIDemoNavController導致:

- (void)viewDidLoad{ 
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; 
    [super viewDidLoad]; 
} 

這是完整的演示課程:

#import "UIDemoNavController.h" 

@interface UIDemoNavController() 

@end 

@implementation UIDemoNavController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) {} 
    return self; 
} 

- (void)viewDidLoad{ 
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning{ 
    [super didReceiveMemoryWarning]; 
} 

@end