简体   繁体   中英

UIPopoverPresentationController doesn't disable tap gesture on title view

I have view controller with navigation bar containing title view which handles tap gesture. Also there is a rightBarButtonItem which shows UIAlertController on iPad as popover. Example code below:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColor.whiteColor;

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"Popover test";
    titleLabel.backgroundColor = UIColor.greenColor;
    titleLabel.userInteractionEnabled = YES;
    [titleLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelPress)]];

    self.navigationItem.titleView = titleLabel;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self
                                                                                           action:@selector(showPopover)];
}

- (void)showPopover {
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil
                                                                        message:nil
                                                                 preferredStyle:UIAlertControllerStyleActionSheet];
    controller.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;

    [controller addAction:[UIAlertAction actionWithTitle:@"One" style:UIAlertActionStyleDefault handler:nil]];
    [controller addAction:[UIAlertAction actionWithTitle:@"Two" style:UIAlertActionStyleDefault handler:nil]];

    [self presentViewController:controller animated:YES completion:nil];
}

- (void)titleLabelPress {
    BOOL isYellow = [((UILabel *)self.navigationItem.titleView).backgroundColor isEqual:UIColor.yellowColor];
    ((UILabel *)self.navigationItem.titleView).backgroundColor = isYellow ? UIColor.greenColor : UIColor.yellowColor;
}

The problem is when popover is presenting I still able to tap on title label and popover won't dismiss. Also if I tap on status bar popover won't dismiss. What could be the reason of that problems?

在此处输入图片说明

在此处输入图片说明

According to an answer at:

UIPopoverController does not dismiss when clicking on the NavigationBar

UIPopoverController seems to add the navigation bar to its passthroughViews array when it is presented.

The solution is to do:

[self presentViewController:controller animated:YES completion:^{
    controller.popoverPresentationController.passthroughViews = nil;
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM