1

我有視圖控制器,其導航欄包含處理輕擊手勢的標題視圖。另外還有一個rightBarButtonItem,它在iPad上顯示UIAlertController作爲彈出窗口。示例代碼如下:UIPopoverPresentationController不會在標題視圖上禁用輕擊手勢

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

問題是當popover呈現時,我仍然能夠點擊標題標籤和彈出窗口不會解僱。另外,如果我點擊狀態欄popover不會解僱。這個問題的原因是什麼?

enter image description here

enter image description here

回答

相關問題