您的当前位置:首页正文

IOS 关于导航栏透明

2024-12-15 来源:东饰资讯网

第一步、在需要导航栏透明的页面进行设置

//允许半透明状态

[self.navigationController.navigationBar setTranslucent:YES];

//画一张宽为屏幕宽,高为64的透明图片

UIColor *color = [UIColor clearColor];

CGRect rect = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, 64);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

//将透明图片设为导航栏的背景图片

[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];

[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];

//对超出父视图的子视图不进行裁剪

self.navigationController.navigationBar.clipsToBounds = YES;

第二步、将导航栏改为不透明

[self.navigationController.navigationBar setTranslucent:NO];

CGRect rect = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, 64);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [ColorRGB(41.0, 41.0, 41.0, 1) CGColor]);

CGContextFillRect(context, rect);

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[self.navigationController.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];

self.navigationController.navigationBar.clipsToBounds = NO;

显示全文