您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页IOS 关于导航栏透明

IOS 关于导航栏透明

来源:伴沃教育

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

//允许半透明状态

[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;

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务