0

The following code should popover a view and the view size should be customized as I customized it by code, but when I run it, I see it's not customized.

Can anyone help?

- (IBAction)barButtonLeft:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    _myViewController = [storyboard instantiateViewControllerWithIdentifier:@"Sort"];

    _myViewController.modalPresentationStyle = UIModalPresentationPopover;
    self.myViewController.modalPresentationStyle = UIModalPresentationFormSheet;
    self.myViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

   self.myViewController.preferredContentSize = CGSizeMake(139, 70);


    [self presentViewController:_myViewController animated:YES completion:nil];


    UIPopoverPresentationController *popController = [_myViewController popoverPresentationController];
}
Yun CHEN
  • 6,450
  • 3
  • 30
  • 33

1 Answers1

0

You're using _myViewController and self.myViewController together here, which is a little confusing but I assume you have a property, myViewController, backed by an instance variable, _myViewController? I'm therefore assuming both of these things are actually pointing to the same instance.

For clarity I'd recommend sticking with one or the other, probably property access.

If they are both pointing to the same instance, then what the code above is doing is setting a popover presentation style, then setting a form sheet presentation style. The last one you set will "win", so the view controller will be presented as a form sheet (on iPads, this will be a box in the middle of the screen, on iPhones, it will be full screen).

If you are running this code on an iPhone, then popovers don't work by default anyway - they will be presented full screen. To prevent that, you need to set a delegate to the popover presentation controller, implement - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection;, and return UIModalPresentationNone.

You must also set a source view or bar button item on the popover presentation controller before attempting the presentation, or your app will crash.

jrturton
  • 118,105
  • 32
  • 252
  • 268