更改UINavigationBar的颜色

anc*_*evv 0 iphone objective-c uinavigationbar uinavigationcontroller

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
{       
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                          target:self
                                                                                          action:@selector(cancel)];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Send to Twitter"
                                                                              style:UIBarButtonItemStyleDone
                                                                             target:self
                                                                             action:@selector(save)];

}
return self;
}
Run Code Online (Sandbox Code Playgroud)

另一段代码

 - (void)loadView 
    {
[super loadView];

self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.view.backgroundColor = [UIColor whiteColor];

self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.delegate = self;
textView.font = [UIFont systemFontOfSize:15];
textView.contentInset = UIEdgeInsetsMake(5,5,0,0);
textView.backgroundColor = [UIColor whiteColor];    
textView.autoresizesSubviews = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[self.view addSubview:textView];
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将导航栏颜色更改为黑色,但无论我在这里做什么,颜色仍然保持默认(蓝色).如何更改视图顶部栏的颜色?

在此输入图像描述

Sco*_*bes 6

尝试类似的东西:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

应该是这样的对viewWillAppear:作为在init方法self.navigationController会获得nil,因此不会产生任何影响.

希望这可以帮助.

  • 如果它在`init`方法中,则`self.navigationController`将为`nil`.这应该像`-viewWillAppear:`或类似的方法. (2认同)