Dynamic Height of UITableView

余生长醉 提交于 2021-01-27 07:23:14

问题


How to get the Dynamic Height Of UITableView with Constraints. It will increase the UITableView Height depending upon Number of row without adding Scrollbar in table. I'm facing problem in adding the TableView in UIViewController and Height of TableView Should not be Statically fixed.


回答1:


The most concise way I've found to do this is by using the contentSize of the table view as the intrinsicContentSize

Your tableview needs to be an actual subclass in which you override both contentSize and intrinsicContentSize like this (Note that the contentSize just has an added observer, its not really overridden):

override var contentSize: CGSize {
        didSet {
            self.invalidateIntrinsicContentSize()
        }
    }

override var intrinsicContentSize: CGSize {
        return contentSize
    }

Now, when you set up your table view, make sure you set its content compression resistance and content hugging priority to required, then it should size itself within your view automatically based on its intrinsic content size.



来源:https://stackoverflow.com/questions/44852663/dynamic-height-of-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!