如何实现自动布局以适合Uiview内部使用Uibuttons的StackView?

发布于 2025-02-13 01:33:01 字数 1151 浏览 0 评论 0原文

我通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2025-02-20 01:33:01

问题不是您的身高,它的宽度。
它不起作用,因为您的主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.

enter image description here

Don't forget to set the aspect ratio of your buttons

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文