带有嵌入式导航控制器的弹出框不考虑后导航的大小

Popover with embedded navigation controller doesn#39;t respect size on back nav(带有嵌入式导航控制器的弹出框不考虑后导航的大小)

本文介绍了带有嵌入式导航控制器的弹出框不考虑后导航的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIPopoverController 托管一个 UINavigationController,它包含一个小的视图控制器层次结构.

I have a UIPopoverController hosting a UINavigationController, which contains a small hierarchy of view controllers.

我遵循了文档,对于每个视图控制器,我设置了视图的 popover-context 大小,如下所示:

I followed the docs and for each view controller, I set the view's popover-context size like so:

[self setContentSizeForViewInPopover:CGSizeMake(320, 500)];

(每个控制器的尺寸不同)

(size different for each controller)

当我在层次结构中向前导航时,这将按预期工作——弹出框会自动为大小更改设置动画以对应于推送的控制器.

This works as expected as I navigate forward in the hierarchy-- the popover automatically animates size changes to correspond to the pushed controller.

但是,当我通过导航栏的后退"按钮在视图堆栈中导航后退"时,弹出框的大小不会改变——它仍然与到达的最深视图一样大.这对我来说似乎坏了;我希望弹出框在弹出视图堆栈时尊重设置的大小.

However, when I navigate "Back" through the view stack via the navigation bar's Back button, the popover doesn't change size-- it remains as large as the deepest view reached. This seems broken to me; I'd expect the popover to respect the sizes that are set up as it pops through the view stack.

我错过了什么吗?

谢谢.

推荐答案

我也在为同样的问题苦苦挣扎.以上解决方案都不适合我,这就是为什么我决定做一些调查并找出它是如何工作的.

I was struggling with the same issue. None of the above solutions worked for me pretty nicely, that is why I decided to do a little investigation and find out how this works.

这是我发现的:

  • 当您在视图控制器中设置 contentSizeForViewInPopover 时,弹出框本身不会更改它 - 即使在导航到不同控制器时弹出框大小可能会发生变化.
  • 当导航到不同的控制器时弹出框的大小会发生变化,而返回时,弹出框的大小不会恢复
  • 在 viewWillAppear 中更改弹出框的大小会产生非常奇怪的动画(假设您在弹出框内使用 popController) - 我不推荐它
  • 对我来说,在控制器内部设置硬编码大小根本不起作用 - 我的控制器有时必须大有时小 - 向它们展示的控制器虽然对大小有所了解
  • When you set the contentSizeForViewInPopover in your view controller it won't be changed by the popover itself - even though popover size may change while navigating to different controller.
  • When the size of the popover will change while navigating to different controller, while going back, the size of the popover does not restore
  • Changing size of the popover in viewWillAppear gives very strange animation (when let's say you popController inside the popover) - I'd not recommend it
  • For me setting the hardcoded size inside the controller would not work at all - my controllers have to be sometimes big sometimes small - controller that will present them have the idea about the size though

解决所有这些痛苦的方法如下:

A solution for all that pain is as follows:

您必须在 viewDidAppear 中重置 currentSetSizeForPopover 的大小.但是您必须小心,当您设置的大小与字段 currentSetSizeForPopover 中已设置的大小相同时,弹出框不会更改大小.为此,您可以首先设置假尺寸(与之前设置的不同),然后设置正确的尺寸.即使您的控制器嵌套在导航控制器中,此解决方案也可以工作,并且当您在控制器之间导航时,弹出框会相应地更改其大小.

You have to reset the size of currentSetSizeForPopover in viewDidAppear. But you have to be careful, when you will set the same size as was already set in field currentSetSizeForPopover then the popover will not change the size. For this to happen, you can firstly set the fake size (which will be different than one which was set before) followed by setting the proper size. This solution will work even if your controller is nested inside the navigation controller and popover will change its size accordingly when you will navigate back between the controllers.

您可以使用以下帮助方法轻松在 UIViewController 上创建类别,该方法可以设置大小:

You could easily create category on UIViewController with the following helper method that would do the trick with setting the size:

- (void) forcePopoverSize {
    CGSize currentSetSizeForPopover = self.contentSizeForViewInPopover;
    CGSize fakeMomentarySize = CGSizeMake(currentSetSizeForPopover.width - 1.0f, currentSetSizeForPopover.height - 1.0f);
    self.contentSizeForViewInPopover = fakeMomentarySize;
    self.contentSizeForViewInPopover = currentSetSizeForPopover;
}

然后只需在所需控制器的 -viewDidAppear 中调用它.

Then just invoke it in -viewDidAppear of desired controller.

这篇关于带有嵌入式导航控制器的弹出框不考虑后导航的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有嵌入式导航控制器的弹出框不考虑后导航的大小

基础教程推荐