当前位置: 首页 > news >正文

导航栏渐变色iOS


- (void)viewDidLoad {[super viewDidLoad];// 设置导航栏属性self.navigationBar.translucent = NO;[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:28]}];// 修复iOS 15后导航栏变白色的bug并设置渐变背景[self ios15nvbug];
}
- (UIImage *)gradientImageWithBounds:(CGRect)bounds {CGSize size = bounds.size;if (size.width == 0 || size.height == 0) {size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 88); // 默认宽度和导航栏高度}UIGraphicsBeginImageContext(size);CGContextRef context = UIGraphicsGetCurrentContext();// 创建渐变CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();CGFloat locations[] = {0.0, 1.0};NSArray *colors = @[(__bridge id)[UIColor colorWithRed:244/255.0 green:102/255.0 blue:36/255.0 alpha:1].CGColor,(__bridge id)[UIColor colorWithRed:205/255.0 green:75/255.0 blue:0 alpha:1].CGColor];CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);// 横向渐变(从左到右)CGPoint startPoint = CGPointMake(0, 0);CGPoint endPoint = CGPointMake(size.width, 0);CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);// 获取渐变图像UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();CGGradientRelease(gradient);CGColorSpaceRelease(colorSpace);return gradientImage;
}
- (void)ios15nvbug {if (@available(iOS 13.0, *)) {UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];[appearance configureWithOpaqueBackground];// 设置渐变背景图片UIImage *gradientImage = [self gradientImageWithBounds:self.navigationBar.bounds];appearance.backgroundImage = gradientImage;appearance.shadowImage = [[UIImage alloc] init];appearance.shadowColor = nil;// 设置导航栏标题的文本属性[appearance setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont boldSystemFontOfSize:22]}];self.navigationBar.standardAppearance = appearance;self.navigationBar.scrollEdgeAppearance = appearance;} else {// iOS 13 以下使用旧的方法设置背景图片UIImage *gradientImage = [self gradientImageWithBounds:self.navigationBar.bounds];[self.navigationBar setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];[self.navigationBar setShadowImage:[UIImage new]]; // 去除默认的阴影线[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName: [UIFont boldSystemFontOfSize:22]}];}
}// 其余方法保持不变...@end

关键点解释

  1. 生成渐变背景图片 (gradientImageWithBounds: 方法):

    • 使用 UIGraphicsBeginImageContextCGGradientRef 生成一张横向渐变的图片。
    • 渐变颜色从左到右的效果通过设置 startPointendPoint 来实现。
    • 通过 UIGraphicsGetImageFromCurrentImageContext 获取生成的渐变图片。
  2. 设置导航栏的背景图片:

    • 使用 setBackgroundImage:forBarMetrics: 方法将生成的渐变图片设置为导航栏的背景,这样可以确保其他 UI 元素(如标题和按钮)不会被遮挡。
  3. 修复导航栏在 iOS 15 中的显示问题:

    • ios15nvbug 方法中设置 UINavigationBarAppearance,确保导航栏背景透明,以便使用渐变背景图片。

http://www.lryc.cn/news/475425.html

相关文章:

  • mysql读写分离
  • 计算机的错误计算(一百四十二)
  • 利用大模型辅助科研论文写作·第一期|论文写作·24-11-02
  • JavaScript。—关于语法基础的理解—
  • Tomcat 11 下载/安装 与基本使用
  • Linux系统时间服务——Chrony服务器
  • C# 接口(Interface)
  • 《高频电子线路》—— 电容三端LC振荡器
  • leetcode35.搜索插入位置
  • Redis全系列学习基础篇之位图(bitmap)常用命令的解析
  • Copilot功能
  • 《GBDT 算法的原理推导》 11-13初始化模型 公式解析
  • # Easysearch 与 LLM 融合打造高效智能问答系统
  • 本地可以插入表记录,生产不能插入表记录
  • 11.Three.js使用indexeddb前端缓存模型优化前端加载效率
  • 功能测试:方法、流程与工具介绍
  • 【Orange Pi 5 Linux 5.x 内核编程】-设备驱动中的sysfs
  • 微信小程序-全局数据共享/页面间通信
  • java计算机毕设课设—Java聊天室(附源码、文章、相关截图、部署视频)
  • 图像识别基础认识
  • 使用 OpenCV 读取和显示图像与视频
  • 【1】Elasticsearch 30分钟快速入门
  • 教材管理系统设计与实现
  • 软考(中级-软件设计师)数据库篇(1101)
  • 安装nscd及glibc包冲突降级【centos7】
  • Qt字符编码
  • Ubuntu用docker安装AWVS和Nessus(含破解)
  • tauri开发中如果取消了默认的菜单项,复制黏贴撤销等功能也就没有了,解决办法
  • HNU-小学期-专业综合设计
  • Linux安装es和kibana