Dynamic UITableView height

后端 未结 11 1625
無奈伤痛
無奈伤痛 2020-12-29 09:47

I would like to set the UITableView to match the height for all the contents in the table view.

This is my storyboard

The problem with this is the t

11条回答
  •  Happy的楠姐
    2020-12-29 10:07

    Update for Swift 5. Adding maxHeight so that you can specify how tall you want your tableView to be

    class SelfSizingTableView: UITableView {
        var maxHeight = CGFloat.infinity
    
        override var contentSize: CGSize {
            didSet {
                invalidateIntrinsicContentSize()
                setNeedsLayout()
            }
        }
    
        override var intrinsicContentSize: CGSize {
            let height = min(maxHeight, contentSize.height)
            return CGSize(width: contentSize.width, height: height)
        }
    }
    

提交回复
热议问题