iOS开发iOS笔记本

iOS导航栏透明的处理

2016-02-22  本文已影响528人  Alife丶

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

没办法看了很多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

上一篇下一篇

猜你喜欢

热点阅读