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. 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
UIPopoverController seems to add the navigation bar to its passthroughViews array when it is presented.
The solution is to do:
[self presentViewController:controller animated:YES completion:^{
controller.popoverPresentationController.passthroughViews = nil;
}];