UINavigationBar透明或纯色效果

2016-08-05  本文已影响309人  Manba_小洛

需求一:导航栏透明


1 设置透明, 在viewWillAppear方法中设置

-(void)viewWillAppear:(BOOL)animated{
  //设置导航栏 透明效果
  [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  // 线条背景透明  
  self.navigationController.navigationBar.shadowImage = [UIImage new];
}

2 还原系统默认, 在视图将要消失的时候viewWillDisappear方法中设置

-(void)viewWillDisappear:(BOOL)animated{
  // 导航栏背景还原默认
  [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  // 导航栏线条还原默认
  self.navigationController.navigationBar.shadowImage = nil;
}

需求二:导航栏背景需要有纯颜色,但是要隐藏下面的线条

+(UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); //宽高 1.0只要有值就够了
UIGraphicsBeginImageContext(rect.size); //在这个范围内开启一段上下文
CGContextRef context = UIGraphicsGetCurrentContext();    
CGContextSetFillColorWithColor(context, [color CGColor]);//在这段上下文中获取到颜色UIColor
CGContextFillRect(context, rect);//用这个颜色填充这个上下文
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//从这段上下文中获取Image属性,,,结束
UIGraphicsEndImageContext();
return image;
}
上一篇 下一篇

猜你喜欢

热点阅读