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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,693评论 0 3
  • 转自:http://www.code4app.com/blog-866962-1317.html1、设置UILab...
    MMOTE阅读 1,663评论 1 1
  • 原文 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新。 1.UITableView的Group...
    无沣阅读 792评论 0 2
  • 嘘!我的灵魂 保持静默 永远都不必去走近一个人 哪怕是你寄居多年的身体 他们深谙规则 这些古老而无趣的真理 已在他...
    f3494c632b49阅读 816评论 34 45
  • 她代替弟弟从军,奋勇杀敌,后,传来噩耗说她战死沙场,死后,她的家人受赏,皇帝甚至亲自为弟弟赐婚, 然而,就在弟弟的...
    我跟你去二次元阅读 380评论 5 2