In meinem Fall ist das Problem anders, das Popover wird für ein UIBarButtonItem mit einer benutzerdefinierten Ansicht angezeigt. Wenn Sie für iOS 11 die benutzerdefinierte Ansicht von UIBarButtonItem verwenden, muss die benutzerdefinierte Ansicht automatisch Layout-freundlich sein.
Mit dieser Kategorie können Sie schnell die Einschränkungen anwenden.
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
Dann können Sie tun:
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];
Eine Zeit, die Sie anwenden, um die Einschränkungen der popover gezeigt korrekt über der benutzerdefinierten Ansicht, z. B. der Code zum Anzeigen einer Warnung als Popover ist:
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];
https://www.dropbox.com/s/99qjhqna07t1iwc/popover.png?dl=0 – user3642915