代码之家  ›  专栏  ›  技术社区  ›  Ben Scheirman

隐藏导航栏和选项卡栏时,UIView不会调整为全屏

  •  37
  • Ben Scheirman  · 技术社区  · 15 年前

    导航栏&选项卡栏是隐藏的,我将文本视图的框架设置为全屏。问题是,选项卡栏原来所在的位置有大约50px的空白。您可以从这个屏幕截图中看到:

    拆下固定的影像棚链接

    我不确定是什么原因造成的。空白绝对不是文本视图后面的视图,因为我将其背景色设置为红色只是为了确定。这可能是什么原因造成的?

    我在UIWindow子类中做了一些命中测试,发现空白实际上是未记录/未发布的UILayoutContainerView。这是选项卡栏的父视图。我认为不建议直接操作此视图,因此如何隐藏选项卡栏?

    **更新#2**

    全屏显示后,视图的边框只有411像素高。我尝试过手动处理框架,也尝试过设置autoResizeMask,但没有成功。

    ****更新:以下是最终结果****

    - (void)toggleFullscreen {
        isFullScreen = !isFullScreen;  //ivar
        
        //hide status bar & navigation bar
        [[UIApplication sharedApplication] setStatusBarHidden:isFullScreen animated:YES];
        [self.navigationController setNavigationBarHidden:isFullScreen animated:YES];
        
        [UIView beginAnimations:@"fullscreen" context:nil];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:.3];
        
        //move tab bar up/down
        CGRect tabBarFrame = self.tabBarController.tabBar.frame;
        int tabBarHeight = tabBarFrame.size.height;
        int offset = isFullScreen ? tabBarHeight : -1 * tabBarHeight;
        int tabBarY = tabBarFrame.origin.y + offset;
        tabBarFrame.origin.y = tabBarY;
        self.tabBarController.tabBar.frame = tabBarFrame;
        
        //fade it in/out
        self.tabBarController.tabBar.alpha = isFullScreen ? 0 : 1;
        
        //resize webview to be full screen / normal
        [webView removeFromSuperview];
        if(isFullScreen) {
                    //previousTabBarView is an ivar to hang on to the original view...
            previousTabBarView = self.tabBarController.view;
            [self.tabBarController.view addSubview:webView];
    
            webView.frame = [self getOrientationRect];  //checks orientation to provide the correct rect
            
        } else {
            [self.view addSubview:webView];
            self.tabBarController.view = previousTabBarView;
        }
        
        [UIView commitAnimations];
    }
    

    15 回复  |  直到 4 年前
        1
  •  31
  •   Harry    15 年前

    我遇到了这个问题,我分别在屏幕底部和顶部设置了选项卡栏和导航栏的动画,在选项卡栏所在的位置留下了49像素高的空白。

    为了解决这个问题,我简单地添加了新的全屏视图(在您的例子中是包含所有文本的视图)作为UITabBarController视图的子视图。

    [[[self tabBarController] view] addSubview:yourTextView];
    

    然后,您需要做的就是确保子视图的帧为480 x 320px,并且它应该填充屏幕(包括以前神秘的空白区域)

        2
  •  5
  •   Louis Gerbarg    15 年前

    通过移动视图的框架,使选项卡栏脱离屏幕,您完全可以做出正确的显示。我知道有人提到尝试过,而你说它不起作用,但我怀疑问题是你在尝试时没有正确设置textviews resize mask。下面是应该工作的代码:

    - (void)viewDidLoad {
      [super viewDidLoad];
      currentlyHidden = NO;
    }
    
    - (void) hideStuff {
      SO2AppDelegate *appDelegate = (id)[[UIApplication sharedApplication] delegate];
      self.navigationController.navigationBarHidden = YES;
    
      CGRect newFrame = appDelegate.tabBarController.view.frame;
      newFrame.size.height += appDelegate.tabBarController.tabBar.frame.size.height;
      appDelegate.tabBarController.view.frame = newFrame;
    }
    
    - (void) showStuff {
      SO2AppDelegate *appDelegate = (id)[[UIApplication sharedApplication] delegate];
    
      CGRect newFrame = appDelegate.tabBarController.view.frame;
      newFrame.size.height -= appDelegate.tabBarController.tabBar.frame.size.height;
      appDelegate.tabBarController.view.frame = newFrame;
    
      self.navigationController.navigationBarHidden = NO;
    }
    
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
      if (currentlyHidden) {
        [self showStuff];
        currentlyHidden = NO;
      } else {
        [self hideStuff];
        currentlyHidden = YES;
      }
    }
    

    此外,由于这肯定是敏感的,你如何有你的笔尖等设置,我很高兴 posting 一个在线项目,这样你就可以下载并查看它。这是一个非常简单的演示,只是一个基于导航的项目,在该项目中,代理设置一个选项卡控制器,并从主nib嵌入视图控制器。其余部分在RootViewController nib和类中。每当触摸开始时,这些条都会切换,因此只需轻触屏幕即可。显然,在一个真正的应用程序中,你可能需要调整滚动视图,使内容看起来不会跳跃。

        3
  •  3
  •   Daniel Dickison    15 年前

    hidesBottomBarWhenPushed 财产 YES ? 您需要将此设置为 initWithNibName:bundle: initWithCoder:

        4
  •  2
  •   ojreadmore    15 年前

    也许还有另一个视图是选项卡栏区域的内置部分?也就是说,有一些额外的东西需要隐藏。这将帮助您查看周围的视图。

    for (UIView *viewy in [self.navigationController.view subviews])
    {
        NSLog([viewy description]);
    }
    
        5
  •  2
  •   Steve    14 年前

    我发现,如果您标记新视图,您将推到导航堆栈上,只要选项卡栏在堆栈上,下面的一行就会隐藏它。

    scrollViewController.hidesBottomBarWhenPushed = YES;

        6
  •  2
  •   anatoliy_v    10 年前

    我也有类似的问题。正如Harry提到的,导航控制器是选项卡栏控制器的内部控制器。找到了重新绘制视图并获得正确帧的另一种方法。在控制器中添加以下内容:

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        // Here you can make your tabBarControlelr.view.hidde = true or use any animation that hides UITabBar. I made
        [TabBarController setTabBarHidden:YES];
    
        self.navigationController.view.frame = CGRectMake(self.navigationController.view.frame.origin.x, self.navigationController.view.frame.origin.y, self.navigationController.view.frame.size.width, self.navigationController.view.frame.size.height + 50);
        [self.navigationController.view setNeedsLayout];
    
     }
    

    这就是我如何隐藏它。我可以让它直接调用TabBarControlelr的方法或类方法包装器:

    + (void)setTabBarHidden:(BOOL)hidden
    {
        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
        TabBarController *tabBarController = (TabBarController *) delegate.window.rootViewController;
    
        if ([tabBarController isKindOfClass:[TabBarController class]]) {
            [tabBarController setTabBarHidden:hidden];
        }
    }
    
    
    - (void)setTabBarHidden:(BOOL)hidden
    {
        if (self.tabBar.hidden == hidden) {
            return;
        }
    
        if (hidden == NO) {
            self.tabBar.hidden = hidden;
        }
    
        [UIView animateWithDuration:0.15 animations:^{
            self.tabBar.frame = CGRectOffset(self.tabBar.frame, 0, ( hidden ? 50 : -50 ));
        } completion:^(BOOL finished) {
            self.tabBar.hidden = hidden;
        }];
    }
    
        7
  •  1
  •   James Hall    15 年前

    我相信问题在于您仍然在选项卡栏控制器中,我也遇到了一些问题,最后为我的应用程序委托创建了两个视图,选项卡栏控制器和一个单独的uiview,其中包含选项卡栏控制器试图控制的任何内容。您可以将视图传递给应用程序代理并在那里使用它吗?

        8
  •  1
  •       15 年前

    尝试使TabBarControllers视图大于屏幕,以便在屏幕外隐藏选项卡栏,只要您这样做 之前 设置新的ViewController后,其视图将调整大小以填充新的框架!

    在测试中,我使用了tabBarController:shouldSelectViewController:delegate方法来检查哪个视图控制器将成为当前视图控制器,并相应地设置tabBarController.view.frame。例如:

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
    {
        if( viewController == second)
            tabBarController.view.frame = CGRectMake( 0, 20, 320, 500);
        else
            tabBarController.view.frame = CGRectMake( 0, 20, 320, 460);
    }
    

        9
  •  1
  •   Benjie    15 年前

    当您选择全屏显示时,我会将视图移到窗口。例如

    myView = [self.view retain];
    self.view = nil;
    [window addSubview:myView];
    

    回到过去:

    [myView removeFromSuperView];
    self.view = myView;
    [myView release];
    myView = nil;
    

    通过将视图添加为窗口的子视图,它可以完全全屏显示,并位于选项卡栏的顶部。

    然后您可以将其与

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    

    检测旋转。(我认为您可以将其与view.transform=CGAffineTransformMakeRotation结合使用以使其旋转…?)

        10
  •  0
  •   rpetrich    15 年前

    如果在代码中创建UITextView,请确保将框架设置为足够大,并且视图的父视图(及其父视图)也足够大。为了简单起见,直接将视图添加到窗口中,然后从窗口向内移动。

    - (void)moveViewToSuperview:(UIView *)view
    {
        UIView *newSuperview = [[view superview] superview];
        [view removeFromSuperview];
        [newSuperview addSubview:view];
        [newSuperview bringSubviewToFront:view];
    }
    
        11
  •  0
  •   TahoeWolverine    15 年前

    您是否尝试强制文本自身“重写”(重置正在使用的任何对象的.text属性)?我曾经有过很多这样的例子,视图根本不重新检查或重新绘制它们的数据,而强制似乎是唯一的修复方法。如果从用户体验的角度来看这不好,您可以尝试在用户触摸导航/选项卡栏之后,以及它们消失之前,使其透明。这通常会将视图扩展到屏幕的全尺寸。

        12
  •  0
  •   xingzhi.sg    14 年前

    如果UITabBarController由rootViewController(navigationController)推动。您可以尝试以下操作:

    首先隐藏状态栏/选项卡栏;

    [self.navigationController pushViewController:aBlankViewController animated:NO];
    [self.navigationController popViewControllerAnimated:NO];
    

    你应该能够收回空白,尽管在调整过程中屏幕会震动。。。

        13
  •  0
  •   Community CDub    8 年前

    我也遇到过类似的问题,这次讨论很有帮助: UIWebView on iPad size

    Tony 的回答帮助我发现,在创建子视图时,设置类似以下内容的代码很有帮助:

    self.tabBarController.tabBar.frame = self.view.bounds;
    
        14
  •  0
  •   Deco    13 年前

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([[segue identifier] isEqualToString:@"ViewPhoto"]) {
    
        ImageView *vc = [segue destinationViewController];
    
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    
            NSString *Title = [photoNames objectAtIndex:selectedIndex];
            [vc setSelectedTitle:[NSString stringWithFormat:@"%@", Title]];
    
            vc.hidesBottomBarWhenPushed = YES;
    
            }
        }
    
        15
  •  -1
  •   Daniel    15 年前

    将额外的空间量添加到视图的维度中,因此如果它是矩形CGRectMake(0,0320460),则将其设置为CGRectMake(0,0320480)。。。这发生在我身上,我不太清楚为什么,这可能与您将文本视图置于顶部的基础视图有关,但只需添加维度就可以了