ios 如何更改 UINavigationController 背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9285191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:52:30  来源:igfitidea点击:

How to change UINavigationController background color?

iosobjective-ciphonexcodeuinavigationbar

提问by 4mahmoud

I'm able to change the backgroung image of UINavigationControllerby overriding drawRect: method:

我可以UINavigationController通过覆盖 drawRect: 方法来更改背景图像:

@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

the background is what I intended to be and the tintColoras well, but when trying to set a color that isn't existing in UIColorclass it fails and shows strange color:

背景是我想要的tintColor,也是,但是当尝试设置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

How can I force UINavigationBarto show the color I want?

如何强制UINavigationBar显示我想要的颜色?

Note:I'm only having a problem with navigation controller buttons color since the background itself is set to image.

注意:由于背景本身设置为图像,因此我只遇到导航控制器按钮颜色的问题。

回答by mattjgalloway

You need to do this:

你需要这样做:

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

Otherwise you're doing integer arithmetic and you'll end up with 0 for all of them probably. Use floating point arithmetic and you get the values you desire.

否则,您正在执行整数算术,最终可能会得到 0。使用浮点运算,你会得到你想要的值。

回答by Jasmeet

This Works for me

这对我有用

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