PopoverPresentationController displaying modally with UITableViewController on iPhone

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 { /* Create label */ UILabel *label = [UILabel new]; label.text = @"This should be in a popover."; label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; /* Add label to new ViewController */ 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)]]; /* Define ViewController to present as a popover and display */ popoverVC.modalPresentationStyle = UIModalPresentationPopover; popoverVC.preferredContentSize = CGSizeMake(320, 50); [self presentViewController:popoverVC animated:YES completion:nil]; /* Grab handle to ViewController popoverPresentationController after displaying */ 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.

+5
source share
1 answer

The reason for the black color is simple.

Your controller is transparent (you do not set the background color for your view ). The presentation animation displays it in the table view, but as soon as the animation ends, the views are deleted from it, and as a result you get the background color of the window (black by default) and a transparent controller with a black mark.

Set

 popoverVC.view.backgroundColor = [UIColor yellowColor]; 

and you will see that the controller is behaving correctly.

I am not sure why your controller does not appear as a popover, because you are doing everything correctly in accordance with the documentation. However moving

 [self presentViewController:popoverVC animated:YES completion:nil]; 

to be the last line of the method fixes the problem. Note: according to the documentation, this should not even work.

+1
source

Source: https://habr.com/ru/post/1244485/


All Articles