In my case the problem is different, the popover is shown for a UIBarButtonItem with a custom view. For iOS 11 if you use custom view of UIBarButtonItem, the custom view needs to be auto layout friendly.
With this category you can apply quickly the constraints.
UIView+NavigationBar.h
@interface UIView (NavigationBar)
- (void)applyNavigationBarConstraints:(CGFloat)width height:(CGFloat)height;
- (void)applyNavigationBarConstraintsWithCurrentSize;
@end
UIView+NavigationBar.m
#import "UIView+NavigationBar.h"
@implementation UIView (NavigationBar)
- (void)applyNavigationBarConstraints:(CGFloat)width height:(CGFloat)height
{
if (width == 0 || height == 0) {
return;
}
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:height];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:width];
[heightConstraint setActive:TRUE];
[widthConstraint setActive:TRUE];
}
- (void)applyNavigationBarConstraintsWithCurrentSize {
[self applyNavigationBarConstraints:self.bounds.size.width height:self.bounds.size.height];
}
@end
Then you can do:
UIButton *buttonMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonMenu setImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
buttonMenu.frame = CGRectMake(0, 0, 44, 44);
[buttonMenu addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
//Apply constraints
[buttonMenu applyNavigationBarConstraintsWithCurrentSize];
UIBarButtonItem *menuBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonMenu];
One time you apply the constraints the popover is shown correctly over custom view, e.g., the code for showing a alert as popover is:
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Menu" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
//Add actions ....
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.sourceView = buttonMenu;
popController.sourceRect = buttonMenu.bounds;
[self presentViewController:controller animated:YES completion:nil];