1

I am trying to present a popover controller but for some reason I can't get the width/height of the popover to set correctly. Here's the code I am using:

Here's my code:

        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);

But the popover keep coming out like this:

enter image description here

jjatie
  • 5,152
  • 5
  • 34
  • 56
PruitIgoe
  • 6,166
  • 16
  • 70
  • 137
  • This is gonna sound weird but, try something for me. Right before you access `self.view` for the first time, just on a line on it own, add: `[self view];`. I had a similar issue where the view hadn't loaded correctly when the popover first appears - accessing the view using a method like that seems to fix it..... – Jamie - Fenrir Digital Ltd Nov 22 '16 at 15:37
  • No dice. Still getting the same results. – PruitIgoe Nov 22 '16 at 16:20

1 Answers1

2

Just in case someone stumbles across this issue. I was trying to set the preferredContentSize on the UIPopoverPresentationController and not the UIViewController within it. Correct code is below:

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);
PruitIgoe
  • 6,166
  • 16
  • 70
  • 137