iOS开发中自定义状态栏和导航栏的实现方法
来源:爱站网时间:2020-11-04编辑:网友分享
在移动互联网的世界,更新换代的速度是非常快的,成功的项目是当用户看到这些页面或者功能被深深的吸引了,那么iOS开发中自定义状态栏和导航栏的实现方法,大家知道多少呢?一起来看看爱站技术频道整理的内容吧。
在移动互联网的世界,更新换代的速度是非常快的,成功的项目是当用户看到这些页面或者功能被深深的吸引了,那么iOS开发中自定义状态栏和导航栏的实现方法,大家知道多少呢?一起来看看爱站技术频道整理的内容吧。
iOS 自定义状态栏和导航栏
开发IOS APP 经常会根据需求更改状态栏和导航栏,这里整理了几种方法,大家可以看下。
导航栏透明
-(void)viewWillAppear:(BOOL)animated { //viewWillAppear中设置透明 [super viewWillAppear:animated]; [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; //用空图片填充机位透明 [self.navigationBar setShadowImage:[UIImage new]];//naviBar底部的seperatorLine } -(void)viewDidDisappear:(BOOL)animated { //viewWillAppear中设置恢复 [super viewDidDisappear:animated]; [self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; [self.navigationBar setShadowImage:shadowImage]; }
导航栏渐变
barImageView = self.navigationController.navigationBar.subviews.firstObject; -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat minAlphaOffset = - 64; CGFloat maxAlphaOffset = 200; CGFloat offset = scrollView.contentOffset.y; CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset); _barImageView.alpha = alpha; }
状态栏字体颜色改变
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;//黑色 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;//白色
导航栏隐藏
如果导航栏自定义度高,需要完全自己重写,可以隐藏原来的导航栏,并定义一个新的view
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.navigationBarHidden = YES; } -(void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; self.navigationController.navigationBarHidden = NO; } -(void)ys_initNavigationBar { self.navigationController.interactivePopGestureRecognizer.delegate = (id)self; // 保留右滑pop的手势 _naviBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 64)]; _naviBar.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_naviBar]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, _naviBar.height-0.5, self.view.width, 0.5)]; line.backgroundColor = [UIColor colorForHex:@"f0f0f0"]; [_naviBar addSubview:line]; // 返回 backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 20, 44, 44); [backButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backButton]; // 标题 naviLable = [[UIButton alloc] initWithFrame:CGRectMake(44, 20, self.view.width-44*2, 44)]; naviLable.backgroundColor = [UIColor clearColor]; naviLable.font = [UIFont systemFontOfSize:16]; naviLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview: naviLable]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { //navigationBar change CGFloat minAlphaOffset = 0; CGFloat maxAlphaOffset = 40; CGFloat offset = scrollView.contentOffset.y; CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset); _naviBar.alpha = alpha; naviLabel.alpha = alpha; }
以上便是爱站技术频道小编要告知大家的iOS开发中自定义状态栏和导航栏的实现方法,相信你已经了解的很透彻了,如果你还想学习更多知识,欢迎你随时来js.aizhan.com。
下一篇:IOS开发之身份证标识的几个技术