更改 UINavigationBar 的颜色

发布于 2024-11-08 19:21:21 字数 1787 浏览 0 评论 0原文

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

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

在此处输入图像描述

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

Another piece of code

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

I'm trying to change the navigation bar color to black, however no matter what i do here, the color still stays default (Blue). How do you change the color of the bar on top of the view??

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2024-11-15 19:21:21

尝试如下:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

这应该在 viewWillAppear: 上完成,就像在 init 方法 self 期间一样.navigationController 将被获取为 nil,因此不会产生任何效果。

希望这有帮助。

Try something like:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

This should be done on viewWillAppear: as during init method self.navigationController will be obtained as nil and hence will not produce any effect.

Hope this helps.

青衫负雪 2024-11-15 19:21:21

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

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

}

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

If you use a programatic approach (seems like you don't) you could write this code

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

}

It works for me. Hope it helps! ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文