简体   繁体   English

如何更改 UINavigationController 的背景颜色?

[英]How to change UINavigationController background color?

I'm able to change the backgroung image of UINavigationController by overriding drawRect: method:我可以通过覆盖 drawRect: 方法来更改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

the background is what I intended to be and the tintColor as well, but when trying to set a color that isn't existing in UIColor class it fails and shows strange color:背景是我想要的, tintColor也是,但是当尝试设置UIColor class 中不存在的颜色时,它失败并显示奇怪的颜色:

@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 UINavigationBar to 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.注意:我只有导航 controller 按钮颜色有问题,因为背景本身设置为图像。

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. 否则你正在进行整数运算,你最终可能会得到0。 Use floating point arithmetic and you get the values you desire. 使用浮点运算,您可以获得所需的值。

这对我有用

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

You can also use:您还可以使用:

navigationController.view.backgroundColor =.yourColor navigationController.view.backgroundColor =.yourColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM