添加子视图 myNavigationController.view 时关闭 20 像素(y 轴) [英] Off by 20 pixels (y axis) when adding subview myNavigationController.view

查看:29
本文介绍了添加子视图 myNavigationController.view 时关闭 20 像素(y 轴)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a navigation controller with a menu button. When the menu button is pressed, the current view (complete with its navigation bar) should slide to the right, and a menu should simultaneously and contiguously slide in from the left. The details are explained in the comments in the code, below.

它的工作原理除了一方面.当前视图(在本例中为对话")在添加回我实现的容器视图时,必须将其框架设置为y.min"=-20,否则导航栏上方有 20 个像素.但是,当我将其 y.min 设置为 -20 时,这会将其视图中的所有内容向上移动 20 像素.所以当按下菜单按钮时,当前视图中的所有内容都会突然向上跳跃 20 个像素.

It works except in one regard. The current view (in this case, "Conversations"), when added back into the container view I implement, has to have its frame set to "y.min"=-20, or else there are 20 pixels above its navigation bar. However, when I set its y.min to -20, this shifts everything within its view up by 20 pixels. So when the menu button is pressed, everything in the current view suddenly jumps upwards by 20 pixels.

我无法弄清楚为什么会发生这种情况或如何纠正这种情况.也有可能有一种更简单的方法来解决所有这些问题,但我不知道有一种方法.*(*我对第三方文件不感兴趣.我想自己学习这样做.)

I can't figure out why this is happening or how to correct this. It is also possible that there's an easier way to go about all of this, but I'm not aware of one.* (*I'm not interested in third-party files. I want to learn to do this myself.)

代码如下:

- (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];

}];
}

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

Here is an illustration of my two options (click for higher resolution):

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

screen 1) The first screen shows the view before clicking the menu button.

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

The next two screens show two possibilities as the menu transition animation begins.

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

screen 2) This is what the transition looks like when I set presenter.frame.y.min to -20. As you can see, the button in the view has jumped up by 20 pixels.

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

screen 3) This is what the transition looks like when I set presenter.frame.y.min to 0. As you can see, a bar 20 pixels tall is present at the top. The blue color indicates the container view.

推荐答案

前段时间我遇到了这个问题,因为我没有正确创建我的视图控制器树.我的 20px 关闭是由于将 UINavigationController 视图添加为子视图造成的.那一年,我在 WWDC 的实验室中与 Apple 工程师进行了交谈.他们说我错误地使用了导航控制器,它需要是一个顶级构造,你不应该把它放在另一个 UIViewController 中.也就是说,您可以将它们放在容器视图控制器中,例如 UITabBarControllerUISplitViewController.

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.

您的问题不在您发布的代码中,而是在您的视图控制器的架构中.我已将示例应用程序上传到 GitHub,展示了如何创建幻灯片菜单"应用程序,例如 FaceBook 和 StackOverflow iPhone 应用程序.请参阅 https://github.com/GayleDDS/TestNavBarOffBy20.git

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

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

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

现在显示菜单

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

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

这篇关于添加子视图 myNavigationController.view 时关闭 20 像素(y 轴)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆