繁体   English   中英

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

[英]UIPopoverPresentationController doesn't disable tap gesture on title view

我有带有导航栏的视图控制器,该导航栏包含处理点击手势的标题视图。 还有一个rightBarButtonItemUIAlertController iPad上的UIAlertController显示为弹出UIAlertController 下面的示例代码:

- (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;
}

问题是当弹出窗口出现时,我仍然可以点击标题标签,并且弹出窗口不会消失。 另外,如果我点击状态栏,则弹出窗口不会消失。 出现这些问题的原因可能是什么?

在此处输入图片说明

在此处输入图片说明

根据以下答案:

单击NavigationBar时,UIPopoverController不会关闭

UIPopoverController似乎在显示时将导航栏添加到其passthroughViews数组中。

解决方法是:

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

暂无
暂无

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

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