美文网首页iOS
UINavigationController 设置导航背景色 -

UINavigationController 设置导航背景色 -

作者: survivorsfyh | 来源:发表于2021-04-02 15:56 被阅读0次

设置 UINavigationController 导航的背景颜色,起初如下几步骤即可:

self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];

嗯,写完 Run 了一下,emmm 。。。 没效果 。。。WTF
经过尝试后配置如下:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor blackColor]] forBarMetrics:UIBarMetricsDefault];

其中运用到了一个颜色转换成图片的方法,code 入下:

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

注:导航 navigationBar 的 translucent 最好设置成为默认属性的 NO,若设置成 YES 小伙伴们其它页面也最好测一下免得出别的问题

self.navigationController.navigationBar.translucent = NO;

嗯,小结一下继续码 code 去了 。。。


以上便是此次分享的全部内容,希望能对大家有所帮助!

相关文章

网友评论

    本文标题:UINavigationController 设置导航背景色 -

    本文链接:https://www.haomeiwen.com/subject/phkqkltx.html