美文网首页
设置UINavigationBar透明

设置UINavigationBar透明

作者: wrlynxayy | 来源:发表于2016-05-26 22:39 被阅读38次

方法一:

设置的位置是在视情况而定

-(void)viewWillAppear:(BOOL)animated{

           [super viewWillAppear:animated];

           [self navigationBarAlpha:0];

}

-(void)viewWillDisappear:(BOOL)animated{

         [super viewWillDisappear:animated];

         [self navigationBarAlpha:1];

}

-(void)navigationBarAlpha:(float)alpha

{

// [XCRGBColor(52, 51, 57)设置背景颜色

[self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:[XCRGBColor(52, 51, 57) colorWithAlphaComponent:alpha]]

forBarMetrics:UIBarMetricsDefault];

// 去掉bar下面的分割线

self.navigationController.navigationBar.shadowImage = [UIImage new];

// 设置透明度

self.navigationController.navigationBar.translucent = alpha == 0;

}

-(UIImage*) createImageWithColor:(UIColor*) color

{

CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return theImage;

}

方法二:

self.navigationController.navigationBar.translucent=YES;

[self.navigationController.navigationBarsetBackgroundImage:[UIImagenew]

forBarMetrics:UIBarMetricsDefault];

self.navigationController.navigationBar.shadowImage= [UIImagenew];

相关文章

网友评论

      本文标题:设置UINavigationBar透明

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