0

我正在尝试隐藏我的 NavigationBar 和状态栏(向上滑动动画),但遇到了问题。

当状态栏可见时,在 0 点 (x: 0) 处的每个元素的原点表示在状态栏的正下方。但是,当状态栏被隐藏时,0(x:0)点会更新以容纳新的空间,0(x:0)表示屏幕的绝对顶部。

当我隐藏状态栏并旋转到横向时,视图会自动调整大小,并且所有内容都会转移以使用状态栏的空间,并抛出我的动画:

if (![[UIApplication sharedApplication] isStatusBarHidden]) {
            // Change to fullscreen mode
            // Hide status bar and navigation bar
            [[UIApplication sharedApplication] setStatusBarHidden:YES
                                                    withAnimation:UIStatusBarAnimationSlide];
            [UIView animateWithDuration:animationDuration animations:^{
                navBar.frame = CGRectMake(navBar.frame.origin.x,
                                          -navBar.frame.size.height-20,
                                          navBar.frame.size.width,
                                          navBar.frame.size.height);
            } completion:^(BOOL finished) {
                [navBar setHidden:TRUE];
            }];
        } else {
            // Change to regular mode
            // Show status bar and navigation bar

            [navBar setHidden:FALSE];

            [[UIApplication sharedApplication] setStatusBarHidden:NO
                                                    withAnimation:UIStatusBarAnimationSlide];
            [UIView animateWithDuration:animationDuration animations:^{
                navBar.frame = CGRectMake(navBar.frame.origin.x,
                                              0,
                                              navBar.frame.size.width,
                                              navBar.frame.size.height);
            } completion:^(BOOL finished) {

            }];
        }

有什么建议么?

编辑:这是旋转重新布局后屏幕的样子:图片

4

1 回答 1

1

您将屏幕细分为 UIView 区域有点混乱。

When you're in a navigation controller, there are three views:

  • Navigation controller's "root" view
  • Inside that, the NavigationBar (which of course is a UIView
  • Also inside the navigation controller, a "content area" view

So the Nav controller is managing it's own root view. In that it is filling the space with a NavigationBar at the top, and the rest of the area with one big UIView for content.

When you push your view controller onto the navigation stack, the Nav controller is adding your root view as the content. So your entire "self.view" is completely contained within the Nav controller's "content" view.

And so, of course, when the Nav controller hides the navigation bar... the "content" view expands up to fill the space. And then that view tells your view "hey, there's more space than you're using so your view also expands up to completely fill the Nav controller's content view.

So your view's "0" point is always the top of your view. That never changes. What is changing is where "the top of your view" is relative to the top edge of the screen.

If you want your content to remain in same spot on screen when navbar is removed, then you're going to have to account for the fact that your "zero" point is now higher than it was when there was a nav bar pushing the content view down.

于 2013-03-07T11:10:53.070 回答