Xcode 表示没有布局问题,但正如您所见.我已经尝试了一切.Apple docs、YouTube、Google 等.看来我做得对,但也许是我做事的顺序或其他原因导致了这些问题.在尝试了一切之后,我终于让 Xcode 添加缺少的约束
,这是迄今为止最好的结果.我在 9 个 UIImages 中有 9 个按钮,所以我必须对按钮执行与 UIImages 相同的操作.我暂时将 UIImages 放在按钮的顶部,这样我可以更轻松地看到我在做什么.我有2个截图.请指教.
开始
这是在使用 Xcode 的 add missing constraints
选项之后.我想要什么,但没有雪茄.
如果您的部署目标是 iOS 9 或更高版本,则使用 UIStackView
有一个更简单的解决方案.请参阅
如果您更改名称,您可能需要关闭文件(菜单栏 > 文件 > 关闭Main.storyboard")并重新打开它以使所有内容正确更新.
(请注意,这些名称仅在编辑情节提要时使用.无法在运行时获取这些名称.)
好的,现在创建约束.首先将所有左右边缘约束为具有常数值零的最近邻:
接下来,将顶行的顶边和底边约束为具有常数值零的最近邻.如果你想约束到 superview 的顶部边缘,而不是顶部布局指南,你必须一一做.这是第一个的方法:
对顶行的其他两个视图重复此操作.
接下来,将底部行的顶部和底部边缘约束为具有常数值零的最近邻.同样,如果你想限制到超级视图的底部边缘,你必须一次做一个.示例:
对底行中的其他两个视图重复此操作.
请注意,您不必为中间行创建上下约束,因为现在顶部和底部行被约束到中间行.
最后,选择所有视图并创建等宽和等高约束:
在 Assistant 编辑器中使用 Preview 查看结果:
如果您忘记了任何约束,文档大纲将在右上角显示一个红色箭头.单击它以获得一些帮助,以识别缺少的内容.例如,如果您忘记将顶部和底部约束添加到顶部中心视图,则会收到如下错误:
Xcode has said there are no layout issues but as you can see there is. I have tried everything. Apple docs, YouTube, Google, etc. It seems I am doing it right however maybe the order I'm doing things in or something else is causing these issues. After trying everything I finally let Xcode add missing constraints
and it's the best result so far. I have 9 buttons onto of 9 UIImages so I have to do the same thing I do to the buttons as I do with the UIImages. I have temporarily placed the UIImages on top of the buttons so I can see what I'm doing more easily. I've got 2 screenshots. Please advise.
Beginning
This is after using Xcode's add missing constraints
option.
Sorta what I want but no cigar.
If your deployment target is iOS 9 or later, there is a simpler solution using UIStackView
. Please see my other answer.
There are a number of ways you could create this layout with constraints. Here's one.
First, a tip: you can name your views in the document outline. This makes it much easier to understand which views are which and what your constraints are connected to. To name a view, click it in the outline, press return, and type the name. Then the outline can look like this:
If you change the names, you may need to close the file (menu bar > File > Close "Main.storyboard") and reopen it to make everything update properly.
(Note that these names are only used when editing the storyboard. There is no way to get these names at runtime.)
OK, now to create the constraints. Start by constraining all the left-right edges to nearest neighbor with constant value zero:
Next, constrain the top and bottom edges of the top row to nearest neighbor with constant value zero. If you want to constrain to the top edge of the superview, rather than to the top layout guide, you have to do them one by one. Here's how to do the first one:
Repeat for the other two views in the top row.
Next, constrain the top and bottom edges of the bottom row to nearest neighbor with constant value zero. Again, if you want to constrain to the bottom edge of the superview, you have to do them one at a time. Example:
Repeat for the other two views in the bottom row.
Note that you don't have to create top-bottom constraints for the middle row, because the top and bottom rows are now constrained to the middle row.
Finally, select all your views and create equal-width and equal-height constraints:
Check out the result using Preview in the Assistant editor:
If you forget any constraints, the document outline will show a red arrow at the top right. Click it for some help identifying what's missing. For example, if you forget to add the top and bottom constraints to the top-center view, you get an error like this:
这篇关于创建具有自动布局约束的 3x3 网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!