加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:57:38 所属栏目:百科 来源:网络整理
导读:我可以通过覆盖drawRect:方法来更改UINavigationController的背景图像: @implementation UINavigationBar (UINavigationBarCategory)- (void)drawRect:(CGRect)rect { UIImage *img = [UIImage imageNamed: @"navController.png"]; [img drawInRect:CGRect
我可以通过覆盖drawRect:方法来更改UINavigationController的背景图像:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {

    UIImage *img  = [UIImage imageNamed: @"navController.png"];
    [img drawInRect:CGRectMake(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,self.frame.size.height)];
        self.tintColor = [UIColor colorWithRed:(26/255) green:(103/255) blue:(159/255)                      alpha:1];
}

@end

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

注意:我只对导航控制器按钮颜色有问题,因为背景本身设置为图像.

解决方法

你需要这样做:

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

否则你正在进行整数运算,你最终可能会得到0.使用浮点运算,您可以获得所需的值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读