iOS 自定义状态栏和导航栏详细介绍
iOS自定义状态栏和导航栏
开发IOSAPP经常会根据需求更改状态栏和导航栏,这里整理了几种方法,大家可以看下。
导航栏透明
-(void)viewWillAppear:(BOOL)animated{//viewWillAppear中设置透明
[superviewWillAppear:animated];
[self.navigationBarsetBackgroundImage:[UIImagenew]forBarMetrics:UIBarMetricsDefault];//用空图片填充机位透明
[self.navigationBarsetShadowImage:[UIImagenew]];//naviBar底部的seperatorLine
}
-(void)viewDidDisappear:(BOOL)animated{//viewWillAppear中设置恢复
[superviewDidDisappear:animated];
[self.navigationBarsetBackgroundImage:nilforBarMetrics:UIBarMetricsDefault];
[self.navigationBarsetShadowImage:shadowImage];
}
导航栏渐变
barImageView=self.navigationController.navigationBar.subviews.firstObject;
-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
CGFloatminAlphaOffset=-64;
CGFloatmaxAlphaOffset=200;
CGFloatoffset=scrollView.contentOffset.y;
CGFloatalpha=(offset-minAlphaOffset)/(maxAlphaOffset-minAlphaOffset);_barImageView.alpha=alpha;
}
状态栏字体颜色改变
[UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleDefault;//黑色 [UIApplicationsharedApplication].statusBarStyle=UIStatusBarStyleLightContent;//白色
导航栏隐藏
如果导航栏自定义度高,需要完全自己重写,可以隐藏原来的导航栏,并定义一个新的view
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
self.navigationController.navigationBarHidden=YES;
}
-(void)viewDidDisappear:(BOOL)animated{
[superviewDidDisappear:animated];
self.navigationController.navigationBarHidden=NO;
}
-(void)ys_initNavigationBar{
self.navigationController.interactivePopGestureRecognizer.delegate=(id)self;//保留右滑pop的手势
_naviBar=[[UIViewalloc]initWithFrame:CGRectMake(0,0,self.view.width,64)];
_naviBar.backgroundColor=[UIColorwhiteColor];
[self.viewaddSubview:_naviBar];
UIView*line=[[UIViewalloc]initWithFrame:CGRectMake(0,_naviBar.height-0.5,self.view.width,0.5)];
line.backgroundColor=[UIColorcolorForHex:@"f0f0f0"];
[_naviBaraddSubview:line];
//返回
backButton=[UIButtonbuttonWithType:UIButtonTypeCustom];
backButton.frame=CGRectMake(0,20,44,44);
[backButtonaddTarget:selfaction:@selector(goBack:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:backButton];
//标题
naviLable=[[UIButtonalloc]initWithFrame:CGRectMake(44,20,self.view.width-44*2,44)];
naviLable.backgroundColor=[UIColorclearColor];
naviLable.font=[UIFontsystemFontOfSize:16];
naviLabel.textAlignment=NSTextAlignmentCenter;
[self.viewaddSubview:naviLable];
}
-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
//navigationBarchange
CGFloatminAlphaOffset=0;
CGFloatmaxAlphaOffset=40;
CGFloatoffset=scrollView.contentOffset.y;
CGFloatalpha=(offset-minAlphaOffset)/(maxAlphaOffset-minAlphaOffset);
_naviBar.alpha=alpha;
naviLabel.alpha=alpha;
}