Henny Lee Henny Lee - 2 years ago 111
Swift Question

How do I adjust my popover to the size of the content in my tableview in swift?

I'm using

to show my popover. The
used to show as popover is created programmatically and will usually contain 1 to 5 rows. How do I set up this popover to adjust the size to the content of the tableview?

Code for my popover:

if recognizer.state == .Began {
let translation = recognizer.locationInView(view)

// Create popoverViewController
var popoverViewController = UITableViewController()
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.tableView.backgroundColor = UIColor.popupColor()

// Settings for the popover
let popover = popoverViewController.popoverPresentationController!
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRect(x: translation.x, y: translation.y, width: 0, height: 0)
popover.backgroundColor = UIColor.popupColor()

presentViewController(popoverViewController, animated: true, completion: nil)

Answer Source

Checkout the preferredContentSize property of UIViewController:

let height = yourDataArray.count * Int(popOverViewController.tableView.rowHeight)
popOverViewController.preferredContentSize = CGSize(width: 300, height: height)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download