[英]How to keep UITableView centered on X and Y inside an UIStackView
I have an UILabel
and a UITableView
inside an vertical UIStackView
. 我在垂直的
UIStackView
有一个UILabel
和一个UITableView
。 I want this stack view to always remain centered, even when elements are added to the table view. 我希望此堆栈视图始终保持居中,即使将元素添加到表视图中也是如此。
Right now I added a Horizontal in Container
and Vertical in Container
constraints to the stack view. 现在,我在堆栈视图中添加了“
Horizontal in Container
和“ Vertical in Container
约束。 But it still says that the stack view is missing a Y constraint. 但是它仍然说堆栈视图缺少Y约束。 If I add one then it won't be centered anymore.
如果我添加一个,则它将不再居中。
Any idea what constraints should I add to keep it always centered ? 知道应该添加哪些约束以使其始终居中吗?
Here are two screen shots with how I want my stack view and one with what I have now: 这是两个屏幕快照,分别是我希望如何显示堆栈视图和一个屏幕快照,它们分别是:
The second picture I added a vertical space constraint from stack view to title that's why it looks like that. 我在第二张图片中添加了从堆栈视图到标题的垂直空间约束,这就是它看起来像这样的原因。
mmm... I had this very same request from a client some time ago and the thing is that table views are not meant to be used like that because if you add too many items the table is going to grow in both vertical directions and thats not good at all... BUT! 嗯...我前段时间从一个客户那里收到了同样的请求,事实是表视图不应该那样使用,因为如果添加太多项目,表将在垂直方向上增长,这就是一点都不好...但是! as far as I can see, your cells are all the same size so, this is what I did as my table had a few and finite number of rows and all of them were the same size:
据我所见,您的单元格都具有相同的大小,因此,这是我做的,因为我的表有几行且行数有限,并且所有行都是相同的大小:
I added a height constraint to the tableView and throw it as an IBOutlet to the class it belongs to. 我在tableView中添加了一个高度约束,并将其作为IBOutlet扔到它所属的类中。 Once you have the number of rows for your tasks, you can just update the constant value for the tableView height constraint you have by multiplying the number of rows for the fixed height of your cells.
一旦有了任务的行数,就可以通过将行数乘以单元格的固定高度来更新tableView高度约束的常数值。 That way your tableView will have a height defined by the number of rows it contains.
这样,tableView的高度将由其包含的行数定义。 You should probably just dump the stack view, use the tableView by itself and add some size and centering constraints to your tableView having into account this height constraint I mentioned... Specifically:
考虑到我提到的高度限制,您可能应该转储堆栈视图,单独使用tableView并向tableView添加一些大小和居中限制。
Leading/Trailing or Width as you prefer, vertically centered inside container and the height constraint you are going to modify on runtime. 您可以根据需要选择“前导/尾随”或“宽度”,在容器内垂直居中,并在运行时修改高度约束。
By the way I'm looking at it your stackview is occupying the whole screen , I feel you should go with the normal constraints leading to container, trailing to container, bottom space to container, top space to vertical. 顺便说一下, 您的stackview占据了整个屏幕 ,我觉得您应该遵循正常的约束条件,导致容器,尾随容器,底部空间至容器,顶部空间至垂直。
Think about it for a second, if you add these four constraints, your stack view will still be always horizontally and vertically centered, no matter on which device you run it on, because settings these 4 constraints will always ensure the stackview maintains that constant distance from the superview margins which in your case would be 0 for all the constraints 仔细考虑一下,如果添加这四个约束,则无论您在哪个设备上运行,堆栈视图始终始终在水平和垂直居中,因为设置这四个约束将始终确保堆栈视图保持恒定的距离在您的情况下,对于所有约束而言,superview的边距都是0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.