1

我正在尝试展示一个弹出框控制器,但由于某种原因,我无法正确设置弹出框的宽度/高度。这是我正在使用的代码:

这是我的代码:

        UIStoryboard* myStoryboard = self.storyboard;

        ScenarioPopOver* popOver = (ScenarioPopOver*)[myStoryboard instantiateViewControllerWithIdentifier:@"scenarioPopover"];  

        popOver.modalPresentationStyle = UIModalPresentationPopover;
        [self presentViewController:popOver animated:YES completion:nil];

        // configure the Popover presentation controller
        UIPopoverPresentationController *popController = [popOver popoverPresentationController];
        popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
        popController.delegate = self;

        // in case we don't have a bar button as reference
        popController.sourceView = self.view;
        popController.sourceRect = CGRectMake(self.view.center.x - 25.0, 10.0, 50.0, 50.0);

但是popover一直像这样出来:

在此处输入图像描述

4

1 回答 1

2

以防万一有人偶然发现这个问题。我试图在 UIPopoverPresentationController 而不是其中的 UIViewController 上设置 preferredContentSize。正确的代码如下:

UIStoryboard* myStoryboard = self.storyboard;

    ScenarioPopOver* popOver = (ScenarioPopOver*)[myStoryboard instantiateViewControllerWithIdentifier:@"scenarioPopover"];

    popOver.modalPresentationStyle = UIModalPresentationPopover;
    [self presentViewController:popOver animated:YES completion:nil];

    // configure the Popover presentation controller
    UIPopoverPresentationController *popController = [popOver popoverPresentationController];
    popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popController.delegate = self;
    popOver.preferredContentSize = CGSizeMake(400.0, 300.0);

    // in case we don't have a bar button as reference
    popController.sourceView = self.view;
    popController.sourceRect = CGRectMake(self.view.center.x - 150.0, 10.0, 0.0, 0.0);
于 2016-11-25T12:05:38.617 回答