I am having a problem displaying a UIViewController
with a popover presentation from iPhone in a UITableViewController
. The code I used allows me to display a popover from any other UIViewController
, not the UITableViewController
on the iPhone. It works from the UITableViewController
on the iPad.
When the code is executed, the presentation is temporarily temporarily (as expected, if I had not implemented adaptivePresentationStyleForPresentationController
), and then was closed in black, as if some Auto Layout error had occurred (although the console would indicate otherwise).
As I mentioned, the UITableViewController
works on the iPhone below, and works all the time on the iPad. I am sure that I am missing something quite simple. Any ideas?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UILabel *label = [UILabel new]; label.text = @"This should be in a popover."; label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; UIViewController *popoverVC = [UIViewController new]; [popoverVC.view addSubview:label]; label.translatesAutoresizingMaskIntoConstraints = NO; [popoverVC.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; [popoverVC.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; popoverVC.modalPresentationStyle = UIModalPresentationPopover; popoverVC.preferredContentSize = CGSizeMake(320, 50); [self presentViewController:popoverVC animated:YES completion:nil]; UIPopoverPresentationController *popoverController = [popoverVC popoverPresentationController]; if (popoverController == nil) NSLog(@"popoverController is nil"); else { popoverController.delegate = self; popoverController.popoverLayoutMargins = UIEdgeInsetsMake(15, 15, 15, 15); popoverController.sourceView = tableView; popoverController.sourceRect = [tableView rectForRowAtIndexPath:indexPath]; self.definesPresentationContext = YES; } } - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { NSLog(@"%s", __PRETTY_FUNCTION__); return (UIModalPresentationNone); }
FWIW. One way to quickly prototype this code is to add the above to MasterViewController.m
from the new "Master-detailed application" template in Xcode and define this message so as not to click on DetailViewController
:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { return (NO); }
You also need to set MasterViewController.h
to accept the UIPopoverPresentationControllerDelegate
protocol.
source share