Ios UINavigationBar:添加导航栏无法更改颜色或标题
我在我的故事板上的interface builder上添加了一个UINavigationBar,但我想通过编程更改UINavigationBar的颜色和标题,但它不起作用。这是我的密码:Ios UINavigationBar:添加导航栏无法更改颜色或标题,ios,objective-c,uinavigationbar,Ios,Objective C,Uinavigationbar,我在我的故事板上的interface builder上添加了一个UINavigationBar,但我想通过编程更改UINavigationBar的颜色和标题,但它不起作用。这是我的密码: self.navigationController.navigationBar.tintColor = [UIColor redColor]; self.navigationItem.title = @"New Title"; 你们当中有人知道为什么会发生这种情况,或者知道如何解决这个问题吗 非常感谢
self.navigationController.navigationBar.tintColor = [UIColor redColor];
self.navigationItem.title = @"New Title";
你们当中有人知道为什么会发生这种情况,或者知道如何解决这个问题吗
非常感谢您的帮助。试试:
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
当您有权访问引用(并预览)时,也可以在interface builder中执行这些操作。试试下面的代码
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor]};
如果要更改导航栏的背景色,
使用
您还可以使用这些代码在全局中更改导航栏
[[UINavigationBar appearance] setBarTintColor:RGB(248, 248, 248)];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:COLOR_NAV_GLOBAL}];
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
将其放在视图加载方法中。
第一个用于导航栏的着色颜色,第二个用于标题字体颜色。如果在导航根/主视图控制器中执行此操作,则
self.navigationController.navigationBar.barTintColor=[UIColor purpleColor]代码>导航控制器的根视图控制器是什么?不要直接添加导航栏。将视图控制器放入UINavigationController
中。那么你发布的代码就可以工作了。
[[UINavigationBar appearance] setBarTintColor:RGB(248, 248, 248)];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:COLOR_NAV_GLOBAL}];
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:33.0/255.0 green:30.0/255.0 blue:94.0/255.0 alpha:1.0];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];