簡體   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