iosuitableviewuipresentationcontroller

Dynamically Adjust Width of Table View Embedded in Popover to Fit Cell Content


My landscape, iPad app has a table view controller presented as popover from a bar button item.

To fit the popover to the table view size, I do this on the table view controller's code:

override func viewDidLoad()
{
    super.viewDidLoad()

    tableView.estimatedRowHeight = 44.0
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.scrollEnabled = false
}

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    tableView.reloadData()   
    preferredContentSize = tableView.contentSize
}

This works fine for adjusting the popover's height to that of the table view (taking into account number of rows and height of each row).

However, if I set the label of one of the cells to a string that is too long, it will truncate (...), even though there is plenty of room on screen for the popover (and the table view) to grow further horizontally.

I could hard-code some big-enough width, like this:

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    tableView.reloadData() 

    var size = tableView.contentSize
    size.width = 600  
    preferredContentSize = size 
}

...but I would prefer a more dynamic and elegant solution (e.g. minimum width that fits all cells' contents).


Solution

  • Use these for handle them dynamically

    DynamicView.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin .union(UIViewAutoresizing.FlexibleLeftMargin .union(UIViewAutoresizing.FlexibleRightMargin))