UIPopoverPresentationController on iPhone
By David Lashkhi
Starting from iOS 8 Apple presented new UIPopoverPresentationController instead of UIPopoverController. A few days ago I had to implemented one in iPhone app, so I opened the Apple’s documentation and here’s what we see:
As we see there’s clearly said 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 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);Â
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:
 //Prepare the controller you want to be displayed 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);Â
//configure UIPopoverPresentationController UIPopoverPresentationController *cityErrorPopover = cityAlertViewController.popoverPresentationController; cityErrorPopover.delegate = self;Â
 cityErrorPopover.sourceView = self.view;
cityErrorPopover.sourceRect = button.frame; cityErrorPopover.permittedArrowDirections = UIPopoverArrowDirectionUp; cityErrorPopover.backgroundColor = greenNormal;
 // present popup  [self presentViewController:cityAlertViewController animated:YES completion:nil];








