Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you programmatically configure and present a popover on iPhone in iOS 8+

How can you present a UIViewController in a popover on iPhone (all sizes and orientations), in iOS 8 using only Objective-C code, no Story Boards or other Interface Builder artifacts.

like image 205
William Denniss Avatar asked Jul 06 '15 00:07

William Denniss


People also ask

How do I show popover in IOS?

In the Bar Button Item section Change the System Item to Action. Next, Drag a new View Controller from the Object Library on to the Storyboard. Drag a Text View from the Object Library at the top of this View Controller. This View Controller will be displayed in the popover.

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


1 Answers

In iOS 8, you can configure any view controller to be displayed as a popover, like so:

UIViewController* controller = ...; // your initialization goes here

// set modal presentation style to popover on your view controller
// must be done before you reference controller.popoverPresentationController
controller.modalPresentationStyle = UIModalPresentationPopover;
controller.preferredContentSize = CGSizeMake(150, 300);

// configure popover style & delegate
UIPopoverPresentationController *popover =  controller.popoverPresentationController;
popover.delegate = controller;
popover.sourceView = sourceView;
popover.sourceRect = CGRectMake(150,300,1,1);
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;

// display the controller in the usual way
[self presentViewController:controller animated:YES completion:nil];

So that it displays as a popover on iPhone, add this to the UIPopoverPresentationControllerDelegate delegate of the popover (that you set above):

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}
like image 71
William Denniss Avatar answered Sep 28 '22 10:09

William Denniss