【问题标题】:How can I make UIPopoverPresentationController resize on navigation pop?如何使 UIPopoverPresentationController 在导航弹出时调整大小?
【发布时间】:2015-12-19 14:14:55
【问题描述】:

我在 iOS 应用程序中使用UIPopoverPresentationController 作为弹出框。当弹出窗口中的导航控制器推送一个新的视图控制器时,弹出窗口将调整为该视图控制器的preferredContentSize。但是当导航控制器从堆栈中弹出视图控制器时,弹出框不会调整到以前的大小。我怎样才能做到这一点?

this question 的可能副本,但对于现代的UIPopoverPresentationController

更新:请参阅here 以获取说明问题的示例代码。克隆它并在 iPad 模拟器中运行它。点击弹出框按钮,您将获得带有导航控制器的弹出框。点击 Push bar 按钮项,您会在堆栈上获得一个更高的新 VC(大小在导航栏中)。弹出,它不会重新调整大小。

【问题讨论】:

    标签: ios uinavigationcontroller uipopover


    【解决方案1】:

    添加这一行:

    self.navigationController.preferredContentSize = self.preferredContentSize;
    

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

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

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

    【讨论】:

    • 不错。我实际上有一个UINavigationController 的子类,我用它来解决this issue,所以我覆盖了所有push 和pop 的变体,并在那里应用了这个原理,而不是在弹出框的导航控制器中可以拥有的所有VC。跨度>
    • 你是大师。
    【解决方案2】:

    只需添加您的ViewController 下一个代码

    - (void) viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    
        self.navigationController.preferredContentSize = CGSizeMake(200, self.parentViewController.childViewControllers.lastObject.preferredContentSize.height-100);
    }
    

    这里是video。希望这是您期望得到的行为。

    【讨论】:

    • 这看起来很有希望。但是你能不能让它通用,所以你不假设 +/- 100 磅的大小变化?
    • @Mr.Jefferson 你是什么意思承诺? :) 你写了Pop and it doesn't resize back down to what it was. 现在你可以把它调整回原来的大小。我认为这只是你的一个例子来说明问题。而make it generic 的任务并不明确:) 你能提供一些公式或描述弹出窗口应该如何反应吗?因为问题不是通用的...... :)
    • 您的答案假设您要跳到的 VC 比您要跳到的 VC 宽 200 点,短 100 点。你可以重写你的答案以不假设吗?
    • 我使用了来自 github 的 example。如果您将添加到原始项目中,则提供的代码高于弹出框大小将在您弹回时发生变化。附:您以编程方式在push 上为preferredContentSize 的高度添加了+100。所以你只需要更改self.navigationController.preferredContentSize 而不是self.preferredContentSize。但是,如果您将更详细地描述您需要什么 - 我会尝试解决这个问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多