2012-02-14 93 views
4

我能夠通過重寫的drawRect改變UINavigationController的backgroung圖像:方法:如何更改UINavigationController背景顏色?

@implementation UINavigationBar (UINavigationBarCategory) 

- (void)drawRect:(CGRect)rect { 

    UIImage *img = [UIImage imageNamed: @"navController.png"]; 
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
     self.tintColor = [UIColor blueColor]; 
} 

@end 

的背景是什麼,我意在與tintColor爲好,但嘗試設置一種顏色時ISN」牛逼現有UIColor類失敗並顯示出奇異色彩:

@implementation UINavigationBar (UINavigationBarCategory) 

- (void)drawRect:(CGRect)rect { 

    UIImage *img = [UIImage imageNamed: @"navController.png"]; 
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
     self.tintColor = [UIColor colorWithRed:(26/255) green:(103/255) blue:(159/255)      alpha:1]; 
} 

@end 

我怎麼能強迫UINavigationBar顯示我想要的顏色?

注意:我只有導航控制器按鈕顏色的問題,因爲背景本身設置爲圖像。

回答

10

你需要這樣做:

self.tintColor = [UIColor colorWithRed:(26.0f/255.0f) green:(103.0f/255.0f) blue:(159.0f/255.0f) alpha:1.0f]; 

否則,你正在做的整數運算,你會用0可能最終爲他們所有。使用浮點算術,你可以得到你想要的值。

+0

的作品就像一個魅力!謝謝 – 4mahmoud 2012-02-15 14:33:07

7

這對我的作品

self.navigationController.navigationBar.backgroundColor= [UIColor colorWithRed:57.0/255.0 green:158.0/255 blue:209.0/255 alpha:1.0];