美文网首页动画特效
IOS 关于导航栏透明

IOS 关于导航栏透明

作者: Me小酥酥 | 来源:发表于2016-06-29 16:17 被阅读215次

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

//允许半透明状态

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

相关文章

网友评论

    本文标题:IOS 关于导航栏透明

    本文链接:https://www.haomeiwen.com/subject/smrxjttx.html