简体   繁体   English

如何根据内容动态设置表格视图的高度,但它应与底部按钮保持最小距离?

[英]How to set the height of tableview dynamically based on content but it should maintain a minimum distance from bottom button?

I have UITableview as shown in the image and this is at a distance of 20 points from the bottom button. 如图所示,我有UITableview,它与底部按钮的距离为20点。 So it has no height constraint as of now, it is just pinned to with the top view at 0 margin and 20 from the bottom button. 因此,到目前为止,它没有高度限制,只是固定在顶视图的空白处,距底部按钮20处。

Now what I want is to adjust the frame of this tableview if there are only 2-3 cells or to 0 height if there are no cells. 现在,我想要的是在只有2-3个单元格的情况下调整此tableview的框架,或者在没有单元格的情况下将其调整为0的高度。

I am not really sure about the order in which the updateLayoutconstraints and other related methods have to be called.All I am pretty sure about is that I will have to have a height constraint on the tableview which will be connected via code to the xib and I will have to update it once the reloadData on tableView is called. 我不太确定updateLayoutconstraints和其他相关方法的updateLayoutconstraints顺序,我可以肯定的是我必须对tableview设置高度限制,该高度将通过代码连接到xib和一旦调用tableView上的reloadData ,我将必须对其进行更新。

在此处输入图片说明

You are already thinking in the right direction. 您已经在思考正确的方向。

Here are the steps to make it work: 以下是使其工作的步骤:

  1. Change the button top constraint relation to greaterThanOrEqual 将按钮顶部约束关系更改为greaterThanOrEqual
  2. Add a tableView height constraint as @IBOutlet to your ViewController. 将tableView高度约束作为@IBOutlet添加到ViewController。 Change this constraint's priority from Required to High to avoid an Auto Layout error when the contentSize is greater than the available height 当contentSize大于可用高度时,将此约束的优先级从“ Required更改为“ High以避免自动布局错误
  3. Override updateViewConstraints() in your UIViewController and set the height constraint's constant to tableView.contentSize.height 在UIViewController中重写updateViewConstraints()并将高度约束的常量设置为tableView.contentSize.height
  4. Call updateViewConstraints() whenever the number of rows changes 每当行数更改时,调用updateViewConstraints()

问题是您的UIButton的按钮约束是“相等的”,当表格视图高度减小按钮将改变y的位置并上升时,您可以将其更改为“大于”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM