Introduction
Starting from iOS 8, Apple presented new <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverPresentationController_class/">UIPopoverPresentationController</a>
instead of <a href="https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPopoverController_class/index.html">UIPopoverController</a>
. A few days ago, I had to implement one in iPhone app, so I opened the Apple’s documentation and here’s what we see:
As we see, it is clearly said there that we should configure presentationcontroller
after presenting it.
Incorrect Implementation
But this is not correct if you want the popup to be presented in a small view, like on iPad. To do this, you need first to set your controller as UIPopoverPresentationControllerDelegate
and implement:
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}?
After this, we init view controller we want to be presented:
UIViewController *cityAlertViewController = [UIViewController new];
DLpopupView *popupView = [[DLpopupView alloc] initWithFrame:CGRectMake(0.0, 0.0,
[300, 91) andRestorationName:@"DCpopupView"];
cityAlertViewController.view = popupView;
cityAlertViewController.modalPresentationStyle = UIModalPresentationPopover;
cityAlertViewController.preferredContentSize = CGSizeMake(300, 91);
Now, if we follow Apple’s documentation, we need first to present the cityAlertViewController and then make all the preparation for UIPopoverPresentationController:
[self presentViewController:cityAlertViewController animated:YES completion:nil];
UIPopoverPresentationController *cityErrorPopover =
cityAlertViewController.popoverPresentationController;
cityErrorPopover.delegate = self;
cityErrorPopover.sourceView = self.view;
cityErrorPopover.sourceRect = button.frame;
cityErrorPopover.permittedArrowDirections = UIPopoverArrowDirectionUp;
cityErrorPopover.backgroundColor = greenNormal;
But as I said before, this doesn’t work on iPhone. Presenting view controller before the preparations returns a full screen presentation instead of classic popup.
How to Fix It?
The solution is simple— just present view controller after all the preparations are done:
UIViewController *cityAlertViewController = [[UIViewController alloc] init];
DLpopupView *popupView = [[DLpopupView alloc] initWithFrame:CGRectMake(0.0, 0.0,
[300, 91) andRestorationName:@"DCpopupView"];
cityAlertViewController.view = popupView;
cityAlertViewController.modalPresentationStyle = UIModalPresentationPopover;
cityAlertViewController.preferredContentSize = CGSizeMake(300, 91);
UIPopoverPresentationController *cityErrorPopover =
cityAlertViewController.popoverPresentationController; cityErrorPopover.delegate = self;
cityErrorPopover.sourceView = self.view;
cityErrorPopover.sourceRect = button.frame; cityErrorPopover.permittedArrowDirections =
UIPopoverArrowDirectionUp; cityErrorPopover.backgroundColor = greenNormal;
[self presentViewController:cityAlertViewController animated:YES completion:nil];