UITableView的动态高度 [英] Dynamic Height of UITableView

查看:66
本文介绍了UITableView的动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用约束获取UITableView的动态高度.它将根据行数增加UITableView的高度,而无需在表中添加滚动条. 我在UIViewController中添加TableView时遇到问题,并且TableView的高度不应该静态固定.

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.

推荐答案

我发现最简单的方法是将表格视图的contentSize用作intrinsicContentSize

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

您的表视图必须是一个实际的子类,在其中您要像这样覆盖contentSizeintrinsicContentSize(请注意,contentSize只是添加了一个观察者,实际上并没有被覆盖):

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.

这篇关于UITableView的动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆