swift - Xcode:将对象与 View Controller 边距对齐

标签 swift xcode storyboard

我开始在 Apple 指南的帮助下学习 Swift:https://itunes.apple.com/it/book/app-development-with-swift/id1219117996?mt=11

现在我遇到了一个小问题,我想了解如何解决,但从指南来看它不是很清楚。

我在 398 页面:“指导项目:性格测验”。

最初我必须在 View Controller 的所有角落放置 4 个标签。

enter image description here

来自指南:

“To hold your emoji in their respective corners on all screen sizes, you'll need to add two constraints to each label. Begin by selecting the top-left label and clicking the Add New Constraints button. Enable the top and leading constraints and set them both to 0 pixels, ensuring there's no space between the edges of the label and the margins of the view. By default, the top of a view has a 20-pixel margin, and the left and right sides have 16 pixels of margin. So when you enter 0 pixels, you're actually telling the label to position itself 20 pixels from the top and 16 pixels from the left edge of the view. Add these two constraints”

因此,按照指南,我将第一个标签放在左上角,并添加两个约束

enter image description here

enter image description here

这就是结果。标签与安全区域对齐,而不是与 View 边缘对齐。 enter image description here enter image description here

看看另一个项目(不是我的),我认为我需要的两个约束是这些(或类似的东西):

enter image description here

最佳答案

我将其描述为 Xcode 中的一个错误。您要求提供与 margin 相关的约束,但没有得到。这可不太好!

之后您只需编辑约束即可。在最终的屏幕截图中,双击约束。使用弹出菜单将安全区域约束更改为 super View 约束。然后再次从菜单中选择以选中相对于 margin 。那么您可能必须修复常量值。对其他约束也这样做。

要么一开始就不要使用约束弹出框。相反,通过从按钮中按住 Control 键拖动并使用出现的 HUD 来绘制约束。按住 HUD 中的选项以获得 super View 边距限制。

编辑哦,这是另一个解决方法;在形成约束之前,隐藏安全区域布局指南(取消选中此屏幕截图中的第四个复选框):

enter image description here

现在约束弹出框可以正常工作。

关于swift - Xcode:将对象与 View Controller 边距对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51907531/

相关文章:

ios - 自定义附件 View 按钮的操作是什么 - swift

ios - FSCalender 事件在 swift 中显示

ios - 无法将类型 '[String : Any]' 的值转换为预期的参数类型 '[NSAttributedStringKey : Any]'

iphone - 在 Storyboard 中以编程方式访问 UIViewController

iphone - UITextView 或 UILabel 的边框可以在 Storyboard 中设置吗?

swift - 在不同的 Storyboard上打开一个新窗口

swift - 图像全屏查看 - 需要缩放

ios - 确保输入的 Double 具有特定的格式

ios - XCode Objc header 找不到标准内存库

objective-c - 在Interface Builder中制作的UISearchDisplayController的contentView在哪里设置