1

Everybody says UIPopoverController work on iOS 8.

But if i test my app in iPhone simulator it getting crashed and log says:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'

This code works fine on iPad simulator.

My Development Target set to 8.4

UIViewController* viewController = [[UIViewController alloc] initWithNibName:@"StyleMenu" bundle:nil];
    self.stylePopover = [[UIPopoverController alloc] initWithContentViewController:viewController];
    self.stylePopover.popoverContentSize = CGSizeMake(250,200);
    [self.stylePopover presentPopoverFromBarButtonItem:styleBarButton
                               permittedArrowDirections:UIPopoverArrowDirectionUp
                                               animated:YES];

any help would be appreciated.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
mehdok
  • 1,499
  • 4
  • 29
  • 54

3 Answers3

2

UIPopoverController is unavailable on the iPhone. Use the new popoverPresentationController property of your viewController instead

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/index.html

MYViewController * myViewController = [[MYViewController alloc] init];
myViewController.modalPresentationStyle = UIModalPresentationPopover;
myViewController.popoverPresentationController.sourceView = button;
myViewController.popoverPresentationController.sourceRect = button.bounds;
[self presentViewController: myViewController animated: YES completion: nil];
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • tnx, this works fine, BUT if i use it with `UIBarButtonItem` like `viewController.popoverPresentationController.barButtonItem = styleBarButton;` then how to set `sourceRect` ? if i don't set this the popover fill the entire screen! – mehdok Sep 26 '15 at 09:17
  • The `sourceRect` is where the arrow points. To set the size you'll need to set the `preferredContentSize` property of your view controller – Ashley Mills Sep 26 '15 at 09:24
  • ok, `viewController.preferredContentSize = CGSizeMake(250,200);` will not work and `viewController.popoverPresentationController.preferredContentSize`is readonly property. so how can i set the size? – mehdok Sep 26 '15 at 09:32
  • Is your view controller inside a `UINavigationController`? If so you'll need to set the `preferredContentSize` of that – Ashley Mills Sep 26 '15 at 09:42
  • yes, but `self.navigationController.preferredContentSize = CGSizeMake(250,200);` won't work either. please note that i create viewController with a nib file. and `viewController.preferredContentSize = CGSizeMake(250,200);` works fine on iPad, but in iPhone the entire screen will filled. – mehdok Sep 26 '15 at 10:01
2

Yes, it is possible starting from iOS8. I have already gave answer to similar question and even implemented a useful class to work with popovers on iPhone and even put some custom elements there, such as UITableView, WKWebView, you name it.
You can check out my answer and link to class here: https://stackoverflow.com/a/30418212/2924920

Community
  • 1
  • 1
Soberman
  • 2,556
  • 1
  • 19
  • 22
  • your code seems really nice, BUT how can i use it with `UIBarButtonItem` ?where i have not any `sourceView` and `sourceRect` . – mehdok Sep 26 '15 at 10:21
  • Easy. Just use this code: `- (IBAction)didTapBarButtonItem:(UIBarButtonItem *)sender { // Configure, as in my example first, then do this: popoverController.sourceView = nil; popoverController.sourceRect = CGRectMake(sender.width, CGRectGetHeight(self.navigationController.navigationBar.bounds), 0, 0); // Further configurations, as in my example afterwards... }` – Soberman Sep 26 '15 at 10:31
  • ok that worked, but do you have any problem with device rotation? when i rotate to landscape the `popOver` won't show correctly. any help with that? – mehdok Sep 26 '15 at 13:35
0

Unfortunately the UIPopoverController is exclusive for iPads, as per the Apple documentation

Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.

Also a possible duplicate from this question? UIPopoverController for iphone not working?

Community
  • 1
  • 1
SilkyPantsDan
  • 561
  • 4
  • 5