1

我有带有导航栏的视图控制器,其中包含处理点击手势的标题视图。还有一个在 iPadrightBarButtonItem上显示为弹出框。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;
}

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

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

2

根据一个答案:

点击 NavigationBar 时 UIPopoverController 不会关闭

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

解决办法是:

[self presentViewController:controller animated:YES completion:^{
    controller.popoverPresentationController.passthroughViews = nil;
}];
于 2018-03-08T02:48:49.483 回答