更改UINavigationBar的颜色

时间:2011-05-21 04:58:04

标签: iphone objective-c uinavigationcontroller uinavigationbar

- (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;
}

另一段代码

 - (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];
}

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

enter image description here

2 个答案:

答案 0 :(得分:6)

尝试类似:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

这应该在viewWillAppear:上完成,因为init方法self.navigationController将在nil获得,因此不会产生任何影响。

希望这有帮助。

答案 1 :(得分:0)

如果您使用编程方法(似乎没有),您可以编写此代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

// window initialization
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

YourViewController *viewController = [[YourViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] \
initWithRootViewController:viewController];
[viewController release];
[[navigationController navigationBar] setTintColor:[UIColor blackColor]];
[window addSubview:navigationController.view];

[self.window makeKeyAndVisible];

return YES;

}

它对我有用。希望能帮助到你! ;)