添加子视图myNavigationController.view时closures20像素(y轴)

我有一个菜单button的导航控制器。 当按下菜单button时,当前视图(以其导航栏结束)应该向右滑动,并且菜单应该从左侧同时连续滑动。 细节在下面的代码的注释中解释。

它在一个方面除外。 当前视图(在本例中为“Conversations”)被添加到我实现的容器视图中时,必须将其框架设置为“y.min”= – 20,否则其导航栏上方会有20个像素。 但是,当我将y.min设置为-20时,这会将视图中的所有内容都向上移动20个像素。 所以当按下菜单button时,当前视图中的所有内容突然向上跳跃20个像素。

我无法弄清楚为什么会发生这种情况,或者如何解决这个问题。 也可能有更简单的方法去解决所有这些问题,但是我不知道其中的一个。*(*我对第三方文件不感兴趣,我想自己学习。

这里是代码:

- (IBAction) showMenu:(id)sender { // get pointer to app delegate, which contains property for menu pointer AppDelegate *appDelegate = getAppDelegate; //create container view so that current view, along with its navigation bar, can be displayed alongside menu UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 548.0f)]; [container setBackgroundColor:[UIColor blueColor]]; //before presenting menu, create pointer for current view, to be used in completion routine UIView *presenter = self.navigationController.view; //present menu's VC. it is necessary to present its (table) VC, not just add its view, to retain the functionality of its cells [self presentViewController:appDelegate.menuVC animated:NO completion: ^{ //obtain a pointer to the menu VC's view UIView *menuTemp = appDelegate.menuVC.view; //replace menu VC's view with the empty container view appDelegate.menuVC.view = container; //add the menu view to the container view and set its frame off screen [container addSubview:menuTemp]; menuTemp.frame = CGRectMake(-260.0f, 0.0f, 260.0f, 548.0f); //add the the view that was displayed when the user pressed the menu button. set its frame to fill the screen as normal [appDelegate.menuVC.view addSubview:presenter]; presenter.frame = CGRectMake(0.0f, 0.0f, 320.0f, 548.0f); //animate the 2 subviews that were just added; "PRESENTER"'S FRAME IS THE ISSUE [UIView animateWithDuration:25.3f delay:0.0f options:nil animations:^{ [menuTemp setFrame:CGRectMake(0.0f, 0.0f, 260.0f, 548.0f)]; [presenter setFrame:CGRectMake(260.0f, -20.0/* or 0 */f, 320.0f, 548.0f)]; } completion:nil]; }]; } 

这里是我的两个选项(点击更高分辨率)的插图: 视觉

屏幕1)第一个屏幕显示在点击菜单button之前的视图。

接下来的两个屏幕显示菜单过渡animation开始时的两种可能性。

屏幕2)当我将presenter.frame.y.min设置为-20时,这就是转换的样子。 正如你所看到的,视图中的button已经跳起了20个像素。

屏幕3)当将presenter.frame.y.min设置为0时,这就是过渡的样子。正如您所看到的,顶部有一个20像素高的条形。 蓝色表示容器视图。

前一段时间,我没有正确创build我的视图控制器树,我有这个问题。 我20pxclosures是由于添加一个UINavigationController视图作为子视图。 那年,我在WWDC的实验室与苹果工程师进行了交谈。 他们说我正在使用导航控制器不正确,它需要是一个顶级构造,你不应该把它放在另一个UIViewController 。 这就是说你可以把它们放在一个容器视图控制器,如UITabBarControllerUISplitViewController

您的问题不在您发布的代码中,而是在您的视图控制器的体系结构中。 我已经上传了一个示例应用程序GitHub显示如何创build一个“幻灯片菜单”应用程序,如FaceBook和StackOverflow iPhone应用程序。 见https://github.com/GayleDDS/TestNavBarOffBy20.git

示例应用程序在根视图控制器中使用具有容器视图的故事板来pipe理UINavigationController(主视图)和UITableViewController(菜单视图)。

故事板

主视图 现在显示菜单 滑动菜单

请参阅提交消息cfb2efc创build详细信息。 我开始使用Single View Application模板,您需要添加QuartzCore框架。