如何实现自动布局以适合Uiview内部使用Uibuttons的StackView?
我通过XIB创建一个自定义的NUMPAD键盘,并粘在其正确的布局上。
NUMPADVIEW
总共有4行:3行,带5个按钮,最后一行带有4个按钮。
这是它在xib
现在中的外观:

我想用长宽比
1:1和进行所有
能够正确拟合所有按钮,无论uibuttons
回合。 NUMPADVIEW高度
都可以使用。
我在堆栈溢出上找到了一个很好的答案:单击并遵循它。在上部屏幕截图上,您可以根据其最佳答案看到我制定的约束。
我遇到的问题:
uibuttons
不要更改大小并适合numpadview
frame(按高度):

如果我会添加顶部限制gridview
然后长宽比
on uibuttons
不起作用:

我需要numpadview
能够合身所有uibuttons
内部和按钮都应保持圆形,无论numpadview
高度。如何修改自动布局
能够解决此任务?
测试项目要播放: github
I create a custom Numpad keyboard through xib and stuck on its proper layout.
NumpadView
has 4 rows in total: 3 rows with 5 buttons and 1 last row with 4 buttons.
Here is how it looks in a xib
now:
I want to make all the UIButtons
round with Aspect Ratio
1:1 and for NumpadView
to be able to fit all buttons properly regardless of the height
it can be initialized with.
I found a great answer with a similar problem on Stack Overflow: CLICK and followed it. On the upper screenshot you can see constraints I made based on its best answer.
The problem I have:
UIButtons
don't change the size and fit the NumpadView
frame (by height):
And If I will add a top constraint to a GridView
then Aspect Ratio
on UIButtons
doesn't work:
I need the NumpadView
to be able to fit all UIButtons
inside and the buttons should stay round regardless of NumpadView
height. How can I modify the Auto Layout
to be able to solve this task?
Test project to play with: Github
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不是您的身高,它的宽度。
它不起作用,因为您的主UistackView(垂直)具有前导和尾随约束,而常数为10,它覆盖了您设置的比率并导致按钮的伸展。
解决方案是在UistackView及其监督之间添加中心约束,并将其领先的,尾随的约束更改为
更大或相等的
。这将提供您需要的解决方案。
另外,我注意到您在设置()上设置了按钮Cornerradius,将其移至
override func layoutsubviews()
将解决您可能遇到的另一个问题。The problem is not with your height, its with your width.
It doesn't work because your main UIStackView (vertical) has leading and trailing constraint with constant of 10 it overrides the ratio you set and causes the stretch of the buttons.
The solution is to add a center constraint between that UIStackView and its superview, and change its leading, trailing constraint to
greater or equal
.That will provide the solution you need.
Also, I noticed that you set the button cornerRadius on setup(), Moving it to
override func layoutSubviews()
will fix another issues you may encounter.