这是我的问题
CONTEXT 我有一个ViewController,我有一个效果,当用户在滚动中向下时导航栏变得透明,当用户在滚动视图中上升时导航栏变得正常。我用UIScrollViewDelegate的方法做了这个效果。这是代码:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offset = scrollView.contentOffset.y;
if (scrollView.contentOffset.y < 0){
scrollView.bounces = false;
}else{
scrollView.bounces = true;
}
CGFloat currentAlpha = (offset / 310);
if (currentAlpha < 0) {
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.alpha = 1;
self.navigationController.titleNavBar.alpha = 0; //This property I made in an UINavigationController extension
} else {
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.alpha = 1;
self.navigationController.titleNavBar.alpha = currentAlpha; //This property I made in an UINavigationController extension
[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:0.0f/0.0f green:136.0f/255.0 blue:206.00f/255.0f alpha:currentAlpha]];
}
}
使用上面的代码我得到了效果,但是我有一个问题:我无法将其添加到状态栏,因为self.navigationController.navigationBar.translucent设置为YES。因此,在iPhone中,状态栏显示透明,而iPhone X显示出比其他iPhone更大的透明度(参见图像)。
答案 0 :(得分:1)
您需要使用NavigationBar颜色更改statusBar UIView颜色。
创建AppDelegate SharedInastance:
+ (AppDelegate *)sharedAppDelegate {
return (AppDelegate *)[UIApplication sharedApplication].delegate;
}
在AppDelegate中添加以下代码以获取状态栏视图:
- (UIView *)statusBarView {
return [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
}
如果要更改状态栏颜色,请添加以下代码:
[[AppDelegate sharedAppDelegate] statusBarView].backgroundColor = [UIColor redColor];