2012-03-31 96 views
0

我想更改UINvaigationBar的背景颜色,并使用tintColor属性,但不会更改颜色。您可以结帐下面的代码。更改UINavigationBar的背景颜色

在我的AppDelegate我创造的UITabBarController

tabbar = [[UITabBarController alloc] init]; 

然后,我创建一个UINavigationController和我的UITableViewController重视它

UINavigationController *myNavigation = [[UINavigationController alloc] initWithRootViewController:myViewController]; 

然后附UINavigationControllers我的TabBar这样

[tabbar setViewControllers:viewControllers]; //viewControllers is an array of UINavigationControllers 

我试着设置属性tintColor in myAppdelegate这样

[[myNavigation navigationBar] setTintColor:[UIColor redColor]]; 

但如预期并没有工作,所以我尝试同样在我的ViewController

- (void)viewWillAppear:(BOOL)animated { 
     [super viewWillAppear:animated]; 
     [self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; 
     self.navigationController.navigationBar.barStyle = UIBarStyleBlack; 
     self.navigationItem.title = @"Test"; 
    } 

而且还是没有任何反应。请检查下面的图片,看看现在导航栏的外观。

enter image description here

感谢你的帮助。

干杯 罐子

+0

那么这听起来很奇怪..对我来说[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];这条线很酷.. – 2012-03-31 11:39:19

+0

Upvoted你的问题,所以有人会注意到它::-)) – 2012-03-31 11:39:42

+0

是啊..怪异和奇怪!我试着设置titleView self.navigationItem.titleView = viewForTitle;这是有效的。所以我想引用正确的UINvaigationController对象。 – Jugs 2012-03-31 11:45:43

回答

1

这样的工作,你可以左右可以使用的地方着色颜色的图像请检查下面的代码相同。

if ([myNavigation respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) { 
     [myNavigation setBackgroundImage:[UIImage imageNamed:BAR_IMAGE_NAME] forbarMetrics:UIBarMetricsDefault]; 
    }else { 
     [myNavigation insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_IMAGE_NAME]] autorelease] atIndex:0]; 
    } 
+0

进行编译工作正常!谢谢! – Jugs 2012-04-01 06:34:36