我有一个带菜单按钮的导航控制器。按下菜单按钮后,当前视图(及其导航栏将完整显示)应向右滑动,并且菜单应同时从左侧连续滑动。详细信息在下面的代码注释中说明。

它只在一个方面起作用。将当前视图(在本例中为“对话”)添加回我实现的容器视图时,必须将其框架设置为“y.min” =-20,否则其导航栏上方必须有20个像素。但是,当我将其y.min设置为-20时,这会将其视图中的所有内容上移20个像素。因此,当按下菜单按钮时,当前视图中的所有内容都会突然向上跳跃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];

}];
}

这是我的两个选项的说明(单击以获得更高的分辨率):
ios - 添加 subview myNavigationController.view时偏离20像素(y轴)-LMLPHP

屏幕1)第一个屏幕显示单击菜单按钮之前的视图。

菜单转换动画开始时,接下来的两个屏幕显示了两种可能性。

屏幕2)这是将presenter.frame.y.min设置为-20时的过渡效果。如您所见,视图中的按钮已经跳了20个像素。

屏幕3)这是将presenter.frame.y.min设置为0时的过渡效果。您可以看到,顶部有一个20像素高的条。蓝色表示容器视图。

最佳答案

前一段时间,我没有正确创建视图控制器树时遇到了这个问题。我的20px关闭是由添加UINavigationController视图作为子视图引起的。那年,我在WWDC的实验室中与Apple工程师进行了交谈。他们说我不正确地使用了导航控制器,它必须是顶层构造,您不应将其放在另一个UIViewController中。就是说,您可以将它们放入容器视图控制器中,例如UITabBarControllerUISplitViewController

问题不在于发布的代码中,而在于视图控制器的体系结构中。我已将一个示例应用程序上载到GitHub,其中显示了如何创建“滑动菜单”应用程序(如FaceBook和StackOverflow iPhone应用程序)。参见https://github.com/GayleDDS/TestNavBarOffBy20.git

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

现在显示菜单

有关创建的详细信息,请参见提交消息cfb2efc。我从Single View Application模板开始,您需要添加QuartzCore框架。

关于ios - 添加 subview myNavigationController.view时偏离20像素(y轴),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16601370/

10-10 20:49
查看更多