2012-03-26 101 views
4

這裏是一個的n00b問題,而是一個我似乎無法來解決讀我的書和筆記:的iOS:無法設置導航欄色調顏色

我實現導航控制,我可以」弄清楚爲什麼我的代碼無法爲它設置色調顏色。

在我的應用程序委託實現文件,下applicationDidFinishLaunching:方法:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    rootViewController *rootView = [[rootViewController alloc] initWithNibName:@"rootViewController" bundle:nil]; 
    self.navController = [[UINavigationController alloc] initWithRootViewController:rootView]; 
    self.navController.navigationBar.tintColor = [UIColor colorWithRed:20/255 green:44/255 blue:86/255 alpha:1]; 

navController初始化得很好,但有一個黑色。

+0

心不是'阿爾法:1'完全透明? – cpjolicoeur 2012-03-26 20:37:43

+0

不,一個alpha是完全點亮。 0的Alpha是完全透明的。 – CodaFi 2012-03-26 21:08:13

回答

11

你看到一個黑色的導航欄,因爲[UIColor colorWithRed:20/255 green:44/255 blue:86/255 alpha:1]是黑色的!

你執行整數除法這樣20/255 == 0。表達這些值浮動,你應該看到你所期望的顏色:

[UIColor colorWithRed:20.0/255 green:44.0/255 blue:86.0/255 alpha:1]

+0

我會被詛咒。謝謝!沒有意識到20和20.0之間有區別。 – inorganik 2012-03-26 20:42:45

1

(大部分)色調的顏色只在iOS 5.0+工作(閱讀類參考:))

+0

是的,我正在運行iPhone 5.0模擬器 – inorganik 2012-03-26 20:38:05

+0

嗯,這是我的問題.. – Manuel 2012-03-26 20:41:18

3

這是黑色的,因爲你把整數。

[UIColor colorWithRed:20/255 green:44/255 blue:86/255 alpha:1]; 

嘗試這種情況:

[UIColor colorWithRed:20.0f/255.0f green:44.0f/255.0f blue:86.0f/255.0f alpha:1.0f]; 
1

爲IOS 8.0

self.navController.navigationBar.barTintColor = [UIColor colorWithRed:20/255 green:44/255 blue:86/255 alpha:1];