导航栏渐变色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,确保导航栏背景透明,以便使用渐变背景图片。
相关推荐
未来之窗软件服务13 小时前
操作系统应用开发(七)mac苹果模拟器——东方仙盟练气期
macos·仙盟创梦ide·东方仙盟·系统模拟器
liliangcsdn13 小时前
Mac本地docker安装Kibana+ElasticSearch
elasticsearch·macos·docker
千寻简13 小时前
远程连接Mac操作ClaudeCode一直提示登录Invalid API key · Please run /login
macos
小朋友,你是否有很多问号?13 小时前
Mac安装hadoop
hadoop·macos
00后程序员张16 小时前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
脚踏实地的大梦想家1 天前
【Docker】P2 Docker环境构建准备:MacOS 与 Linux
linux·macos·docker
白玉cfc1 天前
【OC】单例模式
开发语言·ios·单例模式·objective-c
dyxal1 天前
使用tree命令导出文件夹/文件的目录树( Windows 和 macOS)
windows·macos·策略模式
心灵宝贝2 天前
Kite Compositor for Mac v2.1.2 安装教程|DMG文件安装步骤(Mac用户必看)
macos
他们都不看好你,偏偏你最不争气2 天前
【iOS】UIViewController
开发语言·macos·ios·objective-c·cocoa