Varun Naharia Varun Naharia - 3 months ago 125
iOS Question

How to correct UIPopoverController is deprecated warning message?

I am using code this code

mediaLibraryPopover = [[UIPopoverController alloc]
[self.mediaLibraryPopover presentPopoverFromRect:[theButton bounds]

And I am getting this warning in Xcode-7.

UIPopoverController is deprecated ,first deprecated in iOS 9.0 - UIPopoverController is deprecated. Popovers are now implemented as UIViewController presentations. Use a modal presentation style of UIM


You no longer need UIPopoverController for presenting a view controller. Instead you can set the modalPresentationStyle of view controller to UIModalPresentationPopover.

You can use the following code for that:

avc.modalPresentationStyle = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = theButton;
[self presentViewController:avc animated:YES completion:nil];


In a horizontally regular environment, a presentation style where the content is displayed in a popover view. The background content is dimmed and taps outside the popover cause the popover to be dismissed. If you do not want taps to dismiss the popover, you can assign one or more views to the passthroughViews property of the associated UIPopoverPresentationController object, which you can get from the popoverPresentationController property.

In a horizontally compact environment, this option behaves the same as UIModalPresentationFullScreen.

Available in iOS 8.0 and later.

Reference UIModalPresentationStyle Reference

You need to set either sourceView or barButtonItem property, else it will crash with the following message:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (***) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'

For anchoring the popover arrow correctly, you need to specify the sourceRect property also.

avc.modalPresentationStyle                   = UIModalPresentationPopover;
avc.popoverPresentationController.sourceView = self.view;
avc.popoverPresentationController.sourceRect = theButton.frame;
[self presentViewController:avc animated:YES completion:nil];

Refer sourceView and sourceRect for more details.