I have an issue while displaying a UIAlertController
of type UIActionSheet
in iPad. I know iPads need more information in order to display the popover, but I struggle with some strange issues.
I'm checking iOS 13 compatibility issues with my app and it appears the old trick like getting the view with performSelector
on a UIBarButtonItem
doesn't work anymore.
Way 1
So I do this.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIView* internalView = (UIView*) [weakSelf.navigationItem.rightBarButtonItem performSelector:@selector(view)];
menuController.popoverPresentationController.sourceView = weakSelf.navigationController.navigationBar;
menuController.popoverPresentationController.sourceRect = internalView.frame;
menuController.popoverPresentationController.canOverlapSourceViewRect = YES;
}
internalView
seems to be nil instead of getting a UIView in older versions of iOS. The popover displays in top left corner of the screen instead of near the navigation bar button item.
Way 2
So I try another way to do this, the "proper way" in fact, I guess:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
menuController.popoverPresentationController.barButtonItem = weakSelf.navigationItem.rightBarButtonItem;
}
When I try with this code, the app crashes with the message that explains I have to set sourceView/sourceRect/barButtonItem. But I tell it to do it.
Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet from UISmartNavigationController (). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
I try with an UIPopoverPresentationControllerDelegate
as well but I get same results.
I checked and rechecked, weakSelf.navigationItem.rightBarButtonItem
is not nil and set to the right bar button item. I guess I'm missing something huge, but what?