Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize UITableView width to fit its content?

Is there possible way to adjust table view width by width of biggest text that should be set in the cellTitle label?

iOS 8 , SWIFT

like image 523
Nik Avatar asked Nov 01 '22 04:11

Nik


1 Answers

public override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Set the popover tableview width to be max compressed size (assumes all cells visible)
    let maxWidth = tableView.visibleCells.reduce(0.0) { (currentMax, cell) -> CGFloat in
        return max(currentMax, cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).width)
    }

    var contentSize = tableView.contentSize
    contentSize.width = maxWidth
    tableView.contentSize = contentSize
    tableView.bounds.size = contentSize
}
like image 84
GilroyKilroy Avatar answered Nov 15 '22 05:11

GilroyKilroy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!