Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iOS

UIPopoverPresentationController

5.00/5 (4 votes)
15 Nov 2015CPOL 17.3K  
Working with UIPopoverPresentationController

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:

Image 1

As we see, it is clearly said there that we should configure presentationcontroller after presenting it.

Image 2

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:

Objective-C
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}?

After this, we init view controller we want to be presented:

Objective-C
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:

Objective-C
[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:

Objective-C
//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];

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)