iOS [Obj-C] - NavigationBar 透明,滚动时可见项目

iOS [Obj-C] - NavigationBar transparent with visible items while scrolling

这是我的问题

上下文 我有一个 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。因此,在 iPhones 中,状态栏显示透明,在 iPhone 中,X 显示的透明度比另一个 iPhones 更大(见图)。

任何人都知道如何使用导航栏和状态栏实现这种透明效果?

您需要将 statusBar UIView 颜色更改为 NavigationBar 颜色。

  • 创建 AppDelegate 共享实例:

    + (AppDelegate *)sharedAppDelegate {
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
    }
    
  • 在 AppDelegate 中添加以下代码以获取状态栏视图:

     - (UIView *)statusBarView {
    
        return [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    }
    
  • 如果要更改状态栏颜色,请添加以下代码:

    [[AppDelegate sharedAppDelegate] statusBarView].backgroundColor = [UIColor redColor];