2012-02-14 88 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];