Click here to Skip to main content
13,201,940 members (73,275 online)
Click here to Skip to main content
Add your own
alternative version

Stats

7.3K views
3 bookmarked
Posted 15 Nov 2015

UIPopoverPresentationController

, 15 Nov 2015
Rate this:
Please Sign up or sign in to vote.
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:

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:

//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)

Share

About the Author

David Lashkhi
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 16 Nov 2015
Article Copyright 2015 by David Lashkhi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid