iOS导航栏透明的处理

最近碰到项目需求是一个导航栏多种样式的处理 而且有些页面还需要渐变效果

没办法看了很多demo 还是没有符合需求的。但是看了那么多demo也发现并不是很难。所以自己写一个自定义的导航栏


在.h中暴露需要改变的东西

@interface UDNavigationController : UINavigationController

@property (nonatomic, assign) BOOL changing;

@property (nonatomic, strong) UIView *alphaView;//frame为(0,20,[UIScreen mainScreen].bounds.size.width,20)

@property (nonatomic, strong) UIView *statusView;//状态栏,因为有些页面的状态栏是白色 有些是黑色。真是坑爹

@property (nonatomic, strong) UIView *line;//frame为(0,63,[UIScreen mainScreen].bounds.size.width,1)的线条。因为有很多页面的底色是白色 没有这条线很丑、也是被测试说的没办法

//设置透明

-(void)setAlphOpquae;

//设置不透明

- (void)setAlphTransparent;

@end


好啦~开始写.m了

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController{

self = [super initWithRootViewController:rootViewController];

if (self) {

CGRect frame = self.navigationBar.frame;

_alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, frame.size.height )];

_alphaView.clipsToBounds = YES;

_alphaView.backgroundColor = [UIColor whiteColor];

_statusView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];

_statusView.backgroundColor = [UIColor blackColor];

_line = [[UIView alloc]initWithFrame:CGRectMake(0, 63, kScreenWidth, 1)];

_line.backgroundColor = RGBACOLOR(235, 235, 235, 1);

_changing = NO;

[self.view insertSubview:_alphaView belowSubview:self.navigationBar];

[self.view insertSubview:_statusView aboveSubview:self.navigationBar];

[self.view insertSubview:_line aboveSubview:self.navigationBar];

[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"Guide03.jpg"] forBarMetrics:UIBarMetricsCompact];

self.navigationBar.layer.masksToBounds = YES;

[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

self.navigationBar.tintColor = [UIColor whiteColor];

}

return self;

}

//设置透明

-(void)setAlphOpquae{

if (_alphaView.alpha == 1.0) {

return;

}

[UIView animateWithDuration:0.25 animations:^{

_alphaView.alpha = 1.0;

_line.alpha = 1.0;

} completion:^(BOOL finished) {

_alphaView.alpha = 1.0;

_line.alpha = 1.0;

_changing = NO;

}];

}

//设置不透明

- (void)setAlphTransparent{

if(_alphaView.alpha == 0.0){

return;

}

[UIView animateWithDuration:0.25 animations:^{

_alphaView.alpha = 0.0;

_line.alpha = 0.0;

} completion:^(BOOL finished) {

_changing = NO;

_alphaView.alpha = 0.0;

_line.alpha = 0.0;

}];

}

希望以后不会用到这么坑的导航栏T T

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,359评论 1 14
  • 1.NSString过滤特殊字符串定义一个特殊字符的集合NSCharacterSet set = [NSChara...
    奋拓达阅读 4,221评论 0 0
  • 魔幻的背后——读《西游记》 文/李简 花了大半年时间终于把《西游记》看完了。我觉得从好看来评价,最精彩的就是前面几...
    李简jian阅读 2,701评论 0 1
  • 偎在孩子的小床上,我突然发现,已经有好一段时间我们都没有好好聊天了。自从他完全离开我们单独睡之后,我的陪睡...
    芷凡bonita阅读 3,233评论 2 2

友情链接更多精彩内容