在iOS13以上设置系统导航栏背景色
if (@available(iOS 13.0, *)) {
// Create a navigation bar appearance
UINavigationBarAppearance *navigationBarAppearance = [[UINavigationBarAppearance alloc] init];
[navigationBarAppearance configureWithDefaultBackground];
// Create a gradient layer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = @[(id)[UIColor blueColor].CGColor, (id)[UIColor greenColor].CGColor]; // Customize the gradient colors
gradientLayer.startPoint = CGPointMake(0.0, 0.0); // Gradient start point
gradientLayer.endPoint = CGPointMake(1.0, 0.0); // Gradient end point
// Apply gradient layer to the navigation bar background
navigationBarAppearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; // Add blur effect
// Create an image renderer
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(1, 1)];
UIImage *backgroundImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull context) {
gradientLayer.frame = CGRectMake(0, 0, context.format.bounds.size.width, context.format.bounds.size.height);
[gradientLayer renderInContext:context.CGContext];
}];
navigationBarAppearance.backgroundImage = backgroundImage;
// Set the navigation bar appearance
controller.navigationController.navigationBar.standardAppearance = navigationBarAppearance;
controller.navigationController.navigationBar.scrollEdgeAppearance = navigationBarAppearance;
}
效果:

网友评论