2012-06-02 178 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