简体   繁体   English

如何在uistackview中水平居中放置项目

[英]How can I center items horizontally inside uistackview

I have a UIScrollView and an UIStackView. 我有一个UIScrollView和一个UIStackView。 I want to add elements from top to bottom and let the scrollview scroll only vertically. 我想从顶部到底部添加元素,并让scrollview仅垂直滚动。 I have trouble with this and don't really get what the problem is. 我对此有麻烦,但并没有真正弄清问题所在。

So I added the constrains that the UIScrollView should stick to the super view on all sides, added the stackview, you can see its constrain on the pic, also added a label with fixed width. 因此,我添加了UIScrollView应该在所有方面都遵守超级视图的约束,添加了stackview,可以在图片上看到其约束,还添加了宽度固定的标签。 I want this label to be center in the scrollview without the scrollview starting to scroll horizontally. 我希望此标签在滚动视图中居中,而不使滚动视图开始水平滚动。

I think Im missing some constrains? 我认为我缺少一些限制吗? The scroll view also says it has "ambiguous scrollable content width". 滚动视图还说它具有“模糊的可滚动内容宽度”。

在此处输入图片说明

First to fix ambiguous you should create a contentView for the scrollView as Apple recommends and give it width of outmost view then put the stackView inside it and Change alignment of the stackview to .center + alignment of the label itself 首先要解决模棱两可的问题,您应该按照Apple的建议为scrollView创建一个contentView,并为其提供最外视图的宽度,然后将stackView放入其中,并将stackview的对齐方式更改为.center +标签本身的对齐方式

       Scrollview  // pinned to top , leading , trailing ,bottom
          contentView // pinned to top , leading , trailing ,bottom , and have width = outmost view (very important)
             stackView  

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

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