如何在导航弹出窗口上调整UIPopoverPresentationController的大小?

如何在导航弹出窗口上调整UIPopoverPresentationController的大小?

问题描述:

我正在将UIPopoverPresentationController用于iOS应用程序中的弹出窗口.当弹出式窗口中的导航控制器推入新的视图控制器时,弹出式窗口将调整为该视图控制器的preferredContentSize的大小.但是,当导航控制器将视图控制器从堆栈中弹出时,弹出窗口的大小不会重新调整为先前的大小.我该怎么做呢?

I'm using UIPopoverPresentationController for popovers in an iOS app. When a navigation controller in a popover pushes a new view controller, the popover resizes to that view controller's preferredContentSize. But when the navigation controller pops a view controller off the stack, the popover does not resize to the previous size. How can I make it do that?

这个问题的可能重复项,但对于现代的UIPopoverPresentationController.

更新:有关说明问题的示例代码,请参见此处 .克隆它并在iPad模拟器中运行它.点按弹出窗口"按钮,您将获得带有导航控制器的弹出窗口.点击推杆"按钮项,您会在堆栈上获得一个新的更高的VC(大小在导航栏中).弹出,它的大小不会缩小到原来的大小.

Update: See here for example code illustrating the problem. Clone it and run it in an iPad simulator. Tap the Popover button and you get a popover with a nav controller. Tap the Push bar button item and you get a new taller VC on the stack (the size is in the nav bar). Pop and it doesn't resize back down to what it was.

添加此行:

self.navigationController.preferredContentSize = self.preferredContentSize;

到您的viewWillAppear:方法,因此现在看起来像这样:

to your viewWillAppear: method, so that it will now look like this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.navigationController)
    {
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"Push" style:UIBarButtonItemStylePlain target:self action:@selector(onPush)];
        self.navigationItem.rightBarButtonItem = item;
        self.navigationItem.title = NSStringFromCGSize(self.preferredContentSize);
        self.view.backgroundColor = [UIColor lightGrayColor];

        self.navigationController.preferredContentSize = self.preferredContentSize;
    }
}

这将确保每次更改显示的视图控制器时,导航控制器的首选大小都会更新.我在您的示例代码上对其进行了测试,并解决了该问题.

It will make sure that the navigation controller's preferred size is updated each time the displayed view controller changes. I tested it on your sample code and it resolved the issue.