3

我有一个带有菜单按钮的导航控制器。当按下菜单按钮时,当前视图(连同其导航栏)应该向右滑动,并且菜单应该同时并连续地从左侧滑入。详细信息在下面的代码中的注释中进行了解释。

除了一方面,它有效。当前视图(在本例中为“对话”),当添加回我实现的容器视图时,必须将其框架设置为“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];

}];
}

这是我的两个选项的说明(单击以获得更高的分辨率): 视觉的

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

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

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

屏幕 3) 这是当我将 Presenter.frame.y.min 设置为 0 时的过渡效果。如您所见,顶部有一个 20 像素高的条形图。蓝色表示容器视图。

4

1 回答 1

4

I had this problem some time ago when I didn't create my view controller tree correctly. My 20px off was caused by adding a UINavigationController view as a subview. I talked with the Apple Engineers in the labs at WWDC that year. They said I was using the navigation controller incorrectly, it needs to be a top level construct and you shouldn't put it in another UIViewController. That said you can put them in a container view controller like UITabBarController and UISplitViewController.

Your issue is not in the code you've posted it is in the architecture of your view controllers. I've uploaded a sample app to GitHub showing how to create a "Slide Menu" App like the FaceBook and StackOverflow iPhone Apps. See https://github.com/GayleDDS/TestNavBarOffBy20.git

The sample app uses a storyboard with Container Views in the root view controller to manage a UINavigationController (main view) and UITableViewController (menu view).

Storyboard

Main View Now show Menu Slide for Menu

See commit message cfb2efc for creation details. I started with the Single View Application template and you need to add the QuartzCore framework.

于 2013-05-17T05:25:27.093 回答