简体   繁体   English

Xcode:在实用程序/大小/视图中设置宽度和高度,而不是作为约束

[英]Xcode: Setting width and height in Utilities / Size / View versus as a constraint

I'm an iOS and XCode beginner. 我是iOS和XCode的初学者。 I'm trying to set a button to have dimensions of 64 x 64. In searching how this is done, I've come across two different ways: 我试图将按钮的尺寸设置为64 x64。在搜索操作方式时,我遇到了两种不同的方式:

  1. Select the button, and in the Utilities pane's Size inspector's View section, set the height and width. 选择按钮,然后在“实用工具”窗格的“大小”检查器的“视图”部分中,设置高度和宽度。
  2. In the bottom right corner of the storyboard pane, click the "Add New Constraints" button, check "Width" and "Height" and set them to the desired dimensions. 在情节提要面板窗格的右下角,单击“添加新约束”按钮,选中“宽度”和“高度”,并将它们设置为所需的尺寸。 This adds Constraints indented under the button, with height = 64 and width = 64 indented under Constraints . 这增加了Constraints按钮下缩进,具有height = 64width = 64下缩进Constraints

Both seem to work fine, so my question is what is the difference between the two methods, if any? 两者似乎都可以正常工作,所以我的问题是两种方法(如果有)有什么区别?

First of all welcome to iOS development :) 首先欢迎来到iOS开发:)

The answer is: In case 2 you are using Autolayout and in case 1 you are not. 答案是:在情况2中,您正在使用自动版式,在情况1中,您没有使用。

Autolayout is really important and you should definitely always use it. 自动排版非常重要,您应该始终使用它。 The constraints allow you to create "rules" that tell the app how to adjust the UI to different screen sizes. 约束条件使您可以创建“规则”,以告诉应用程序如何将UI调整为不同的屏幕尺寸。 In your case the button might have the same size for all screens, but without setting the constraints the size might change nevertheless depending on the layout of the constraints of other UI Components. 在您的情况下,按钮对于所有屏幕可能具有相同的大小,但是如果没有设置约束,则大小可能会根据其他UI组件约束的布局而改变。

I recommend reading a tutorial about Autolayout. 我建议阅读有关自动布局的教程。 It is easy to learn and hard to master I would say... 我会说,这很容易学习而又很难掌握。

While you're building your interface check the Document Outline to the left of the Storyboard for warnings, usually this is a strong indicator of areas of concern as your application begins to grow. 在构建界面时,请检查情节提要左侧的文档大纲,以获取警告,通常这是随着应用程序开始增长而引起关注的重要指示。 Rule of thumb is when in doubt add constraints. 经验法则是在有疑问时添加约束。

Also worth mentioning for your 64x64 (square) example.. I personally like using a 1:1 aspect ratio constraint plus a height or width constraint so if theres ever a need to scale the view its one constraint value to modify instead of two. 同样值得一提的是您的64x64(正方形)示例。我个人喜欢使用1:1宽高比约束加上高度宽度约束,因此,如果需要缩放视图,则可以修改其一个约束值而不是两个约束值。

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

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