简体   繁体   English

UIPopoverPresentationController不会在标题视图上禁用轻击手势

[英]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. 还有一个rightBarButtonItemUIAlertController iPad上的UIAlertController显示为弹出UIAlertController 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 单击NavigationBar时,UIPopoverController不会关闭

UIPopoverController seems to add the navigation bar to its passthroughViews array when it is presented. UIPopoverController似乎在显示时将导航栏添加到其passthroughViews数组中。

The solution is to do: 解决方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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