简体   繁体   中英

Why is this popover segue centered from a UITableViewController?

I spent a long time trying to create a centered popover from a tableview. I eventually managed to get it working:

在此处输入图片说明

with (i think...) these steps:

  1. Anchor the segue to the Table View
  2. Set "Use Preferred Explicit Size" on the Table View Controller: 在此处输入图片说明
  3. Uncheck all of the "Directions" options in the segue attributes inspector.
  4. Implement the UIPopoverPresentationControllerDelegate protocol in my UITableViewController with the following method:

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { return .None }

Now, I don't think I understand why this worked.

If I leave any of the "Directions" options checked then the popover displays offscreen. Is this because the popover gets positioned outside of the view (which currently occupies the full screen)? How is the anchor point determined?

Is it advisable to use preferred explicit size?

Using the UITableView as an anchor means that when you were using arrows the arrow would position to the edge of the object and then, as you surmised, display the segued-to view controller off screen.

If you want the arrows, instead of tying the segue to the UITableView , you can do as I did (as prompted by another Stack Overflow post) and place a hidden anchor button behind the table view. Then as you touch a cell, move the button to the touch point and then perform the segue.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
CGPoint tapPoint = CGPointMake(cell.frame.origin.x + cell.frame.size.width - (self.hiddenAnchorButton.frame.size.width / 2),
                                cell.center.y + self.tableView.frame.origin.y - self.tableView.contentOffset.y);
self.hiddenAnchorButton.center = tapPoint;

[self performSegueWithIdentifier:@"yourSegueName" sender:cell];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM