iOS AutoLayout Xcode约束教程

Today we will look into iOS AutoLayout in Xcode. We’ll implement and use AutoLayout and Constraint in our storyboards. These components are vital in keeping the storyboards designs consistent across screens of all sizes, by laying out views and defining their positions relative to other views instead of hardcoding.

今天,我们将研究Xcode中的iOS AutoLayout。 我们将在情节提要中实现并使用AutoLayout和Constraint。 通过布局视图并定义它们相对于其他视图的位置而不是硬编码,这些组件对于在所有尺寸的屏幕上保持故事板设计的一致性至关重要。

iOS自动版式 (iOS AutoLayout)

iOS AutoLayout is a system that makes it easy to support multiple screen sizes with just one interface by making the same interface react to changes.

iOS AutoLayout是一种系统,通过使同一个界面对更改做出React,可以轻松地仅通过一个界面来支持多种屏幕尺寸。

AutoLayout does this by setting a set of layout constraints which describe the interface universally for all screens and sizes.

AutoLayout通过设置一组布局约束来做到这一点,这些约束通常描述所有屏幕和尺寸的界面。

Before we get into AutoLayout implementation, let’s see how the storyboards we’ve being creating till now look on different screens.

在开始执行AutoLayout之前,让我们看看到目前为止我们正在创建的情节提要板在不同屏幕上的外观。

Create a new iOS Single View Application named AutoLayouts and open the Main.storyboard and add a UIButton on the ViewController.

创建一个名为AutoLayouts的新iOS Single View应用程序,然后打开Main.storyboard并在ViewController上添加一个UIButton。

As you can see in the above image, when we take the button on the screen, XCode helps us in positioning the button along the blue guidelines.

如您在上图中所看到的,当我们在屏幕上按下按钮时,XCode会帮助我们沿着蓝色准则定位按钮。

We’ll stick with that guideline advice. Now to see the preview screen, instead of building and running the application on the simulator, we’ll open the assistant editor from the top right second button on the toolbar, click on automatic and choose preview from the dropdown. We’ve added three different screen sizes.

我们将坚持该指导意见。 现在要查看预览屏幕,而不是在模拟器上构建和运行应用程序,我们将通过工具栏右上角的第二个按钮打开助手编辑器,单击自动,然后从下拉列表中选择预览。 我们添加了三种不同的屏幕尺寸。

OOPS! The storyboard was displaying the button center horizontally. What’s gone wrong here? We need to set up the AutoLayout and constraints. Following image shows a way to do it.

糟糕! 故事板正在水平显示按钮中心。 这里出了什么问题? 我们需要设置自动版式和约束。 下图显示了一种实现方法。

We need to set two important constraints to the button. We drag it to the open space in the view. We first make it Center Horizontally in the container. You’ll see red lines in the view container(there scary!).

我们需要为按钮设置两个重要的约束。 我们将其拖动到视图中的空白处。 我们首先使其在容器中水平居中。 您将在视图容器中看到红线(有吓人!)。

Those lines denote missing constraints or ambiguities. Clicking on the red arrow in the left panel shows us the error causing those red lines.

这些行表示缺少约束或歧义。 单击左侧面板中的红色箭头会向我们显示导致这些红线的错误。

The error here is that the Y coordinate constraint is not defined. So XCode doesn’t know where to position the button vertically.

这里的错误是未定义Y坐标约束。 因此,XCode不知道垂直放置按钮的位置。

We’ve fix that error by adding another constraint that specifies where the button is positioned vertically as shown in the above image itself.

我们通过添加另一个约束来解决该错误,如上面的图像本身所示,该约束指定了按钮垂直放置的位置。

The constraints setup can be viewed in the left and right panels of the storyboard. The constraints for each view are described in the View Controller Scene hierarchy as shown below.

约束设置可以在情节提要的左右面板中查看。 每个视图的约束在“视图控制器场景”层次结构中进行了描述,如下所示。

Essentially, the constraints on a particular view are described using three colored lines:

本质上,使用三个彩色线描述特定视图的约束:

  1. Blue line : This means all the constraints of the particular view are set correctly

    蓝线 :这表示特定视图的所有约束均已正确设置
  2. Red line : This means that some constraints are missing. There is ambiguity

    红线 :这意味着缺少一些约束。 有歧义
  3. Orange line : This means that the constraints are set but the view position in the interface builder does not correspond to the constraints set. In short, the view is misplaced

    橙色线 :这表示已设置约束,但在界面构建器中的视图位置与设置的约束不对应。 简而言之,视图放错了位置

Before we look into misplaced views we’ll see the constraints present in the right panel as shown below.

在查看放错位置的视图之前,我们将在右侧面板中看到约束,如下所示。

We’ve updated the constraint for vertical spacing of the button by changing the constant value as shown in the above image. We’ll look into details of the other attributes in the next tutorial.

我们已经通过更改常数值(如上图所示)来更新了按钮垂直间距的约束。 在下一个教程中,我们将研究其他属性的详细信息。

Now let’s look into the misplaced views and how we handle them in AutoLayouts.

现在,让我们看看放错位置的视图以及如何在AutoLayouts中处理它们。

Observe closely the above image. We’ve dragged the button whose constraints had already been set.

仔细观察上面的图像。 我们已经拖动了已设置约束的按钮。

The Interface Builder drew two orange boxes: one with a dashed border and one with a solid border. The dashed box displays the views frame according to Auto Layout.

Interface Builder绘制了两个橙色框:一个带有虚线边框的橙色框和一个带有实线边框的框。 虚线框根据“自动布局”显示视图框架。

The solid orange box is the views frame according to how we placed it in the scene.

橙色实心框是根据我们在场景中放置方式的视图框架。

These two should match up, but here they don’t. To match them one way is to Update Frames. Update Frames takes the view back to the position according to the AutoLayout constraints (the orange dotted box). If we wish to keep with the new view positions, just Update Constraints.

这两个应该匹配,但是在这里却不匹配。 匹配它们的一种方法是更新框架 。 更新框架会根据自动版式约束(橙色虚线框)使视图返回到该位置。 如果我们希望保持新的视图位置,只需更新约束

Note: Misplaced views are displayed in the preview in the correct positions (the dotted box one) according to the AutoLayout as shown in the image above.

注意放错视点的视图根据自动布局在预览中的正确位置(虚线框之一)显示,如上图所示。

This brings and end to iOS AutoLayout tutorial. AutoLayout may appear to be scary at times (owing to the red/orange lines). But as we get more versed with it, it’ll be easier to play around with. Compared to defining a view programmatically, AutoLayout save a lot of coding.

这带来并结束了iOS AutoLayout教程。 自动版式有时可能会令人恐惧(由于红色/橙色线)。 但是随着我们对它的深入了解,它会变得更容易玩。 与以编程方式定义视图相比,AutoLayout节省了大量代码。

翻译自: https://www.journaldev.com/10806/ios-autolayout-xcode-constraints-tutorial

你可能感兴趣的:(iOS AutoLayout Xcode约束教程)