最近碰到項(xiàng)目需求是一個(gè)導(dǎo)航欄多種樣式的處理 而且有些頁面還需要漸變效果
沒辦法看了很多demo 還是沒有符合需求的斩祭。但是看了那么多demo也發(fā)現(xiàn)并不是很難腌且。所以自己寫一個(gè)自定義的導(dǎo)航欄
在.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;//狀態(tài)欄,因?yàn)橛行╉撁娴臓顟B(tài)欄是白色 有些是黑色闯团。真是坑爹
@property (nonatomic, strong) UIView *line;//frame為(0,63,[UIScreen mainScreen].bounds.size.width,1)的線條长窄。因?yàn)橛泻芏囗撁娴牡咨前咨?沒有這條線很丑伐谈、也是被測試說的沒辦法
//設(shè)置透明
-(void)setAlphOpquae;
//設(shè)置不透明
- (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;
}
//設(shè)置透明
-(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;
}];
}
//設(shè)置不透明
- (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;
}];
}
希望以后不會(huì)用到這么坑的導(dǎo)航欄T T