导航栏渐变色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,确保导航栏背景透明,以便使用渐变背景图片。
相关推荐
心灵宝贝1 小时前
Kite Compositor for Mac v2.1.2 安装教程|DMG文件安装步骤(Mac用户必看)
macos
他们都不看好你,偏偏你最不争气5 小时前
【iOS】UIViewController
开发语言·macos·ios·objective-c·cocoa
李游Leo8 小时前
Rust 开发环境安装与 crates.io 国内源配置(Windows / macOS / Linux 全流程)
windows·macos·rust
库奇噜啦呼11 小时前
【iOS】UIViewController生命周期
macos·ios·cocoa
开开心心loky13 小时前
[iOS] 属性关键字
macos·ios·objective-c·cocoa·xcode
帅得不敢出门1 天前
macOS苹果电脑运行向日葵远程控制软件闪退
windows·macos·远程控制·向日葵
咕噜签名分发冰淇淋1 天前
苹果ios的系统app应用WebClip免签应用开源及方式原理
ios·开源·cocoa
夏小花花1 天前
Mac m系列芯片向日葵打不开 解决方案
macos·mac
刹_那1 天前
MAC 多个版本 JDK进行切换
macos
应凌云1 天前
Mac环境Neovim 与 LazyVim 安装指南
macos