ios - 如何更改 UINavigationController 背景颜色?

标签 ios objective-c iphone xcode uinavigationbar

我可以通过覆盖 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

背景是我想要的,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

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

注意:我只是遇到了导航 Controller 按钮颜色的问题,因为背景本身设置为图像。

最佳答案

你需要这样做:

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

否则,您在进行整数运算时,很可能所有运算结果都为 0。使用浮点运算并获得所需的值。

关于ios - 如何更改 UINavigationController 背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9285191/

相关文章:

ios - 如何在iOS Objective-c中实现交互式远程通知

objective-c - NULL 和 nil 等价吗?

iOS/SQLite,使用 guid 有效更新大量记录?

ios - 理解 SwiftUI 的显式对齐

ios - map 框。 layerForAnnotation 未被调用。绘图形状

objective-c - 在 Objective C 中通过 POST 上传图片

iphone - 如何从 iOS 中的 Yahoo OAuth 集成获取访问 token ?

iphone - 输入位置时调用号码 - iOS

ios - Obj-C 检查图像是否已存在于照片库中

ios - Uber API 沙箱请求状态未正确更新