2012-03-31 154 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