行业动态
爱站技术频道:iOS自定义状态栏和导航栏开发
2021-03-16

在移动互联网的世界中,更新速度非常快。成功的项目是,当用户看到这些页面或功能被深深吸引时,则IOS自定义开发中的状态栏和导航栏,您知道多少?让我们看一下Aizhan Technology Channel编译的内容。

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;//白色

隐藏导航栏

如果导航栏是高度定制的ios开发自定义导航栏,需要自己完全重写,则可以隐藏原始导航栏并定义新视图

-(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;
}

以上是Aizhan Technology Channel的编辑要告诉您的有关IOS开发中自定义状态栏和导航栏的实现的信息。我相信您已经完全了解它。如果您想了解更多ios开发自定义导航栏,欢迎随时光临。


393701618