代码之家  ›  专栏  ›  技术社区  ›  Javier Landa-Torres

iOS[Obj-C]-导航栏透明,滚动时显示项目

  •  0
  • Javier Landa-Torres  · 技术社区  · 6 年前

    这是我的问题

    上下文 我有一个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。导航栏。“半透明”设置为“是”。因此,在iPhone中,状态栏显示为透明,而在iPhone X中,这种透明性比其他iPhone更大(见图)。

    enter image description here 有人知道如何使用导航栏和Statsus栏来实现这种透明效果吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   iVarun    6 年前

    您需要使用NavigationBar颜色更改statusBar UIView颜色。

    • 创建AppDelegate SharedInstance:

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

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

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

    enter image description here