机动构造

作者: 关于计算机  发布:2019-11-29

iOS Programming Introduction to Auto Layout 自动布局,iosprogramming

iOS Programming Introduction to Auto Layout   自动布局

A single application that runs natively on both the iPad and the iPhone is called a universal application.

贰个原生的能运营在苹果平板 和魅族 的施用叫做universal application 

Then select the Homepwner target in the project and targets list and the General tab. This tab presents a convenient interface for editing some of the target's properties.

图片 1

The table view interface knew how to resize itself for the iPad sized-screen, but your custom detail interface needs some guidance. You will provide that guidance using Auto Layout.

你的table view interface 知道怎样适应华为平板 的显示屏尺寸,然则你和谐的detail interface 须要有的辅导了。你供给运用机关构造来教导你的施用。

1 The Auto Layout System 自动布局种类

you have defined the frames of your views with absolute coordinates either programmatically or by configuring them in Interface Builder.

到近来结束,你定义了你的view 的frame 通过绝对的坐标系通过编制程序或然经过Interface Builder .

Absolute coordinates, however, make your layout fragile because they assume that you know the size of the screen ahead of time.

相对布局,轻巧让您的构造散乱,因为她俩倘使你已经了然了显示器的尺寸。

Using Auto Layout, you can describe the layout of your views in a relative way that allows the frames to be determined at runtime so that the frames' definitions can take into account the screen size of the device that the application is running on.

运用auto layout ,你能表明用意气风发种相持的章程结构你的视图 ,允许frame 在运营时被垄断(monopoly卡塔尔(قطر‎。那样您的采用就可以依据你运维的道具来思索荧屏的尺寸了。

图片 2

Remember that points are used when laying out your interface and map to physical pixels on the device's screen. A retina device has the same size screen in points as a non-retina device, even though it has twice as many pixels.

point not pixels .

2 Alignment rectangle and layout attributes

The Auto Layout system works with yet another rectangle for a view – the alignment rectangle.

auto layout system 如故与另一个rectangle 一齐为view 专门的学业— alignment rectangle.

This

rectangle is defined by several layout attributes 

本条矩形被广大layout attribute属性定义。

图片 3

(1)Width/Height:These values determine the alignment rectangle's size.

以此值决定了alignment rectangle的尺码。

 

(2)Top/Bottom/Left/Right These values determine the spacing between the given edge of the alignment rectangle and the alignment rectangle of another view in the hierarchy.

其后生可畏值决定了在给定的alignment rectangle 和在hierarchy中得其余叁个view的alignment rectangle 的alignment rectangle .

(3)CenterX/CenterY :These values determine the center point of the alignment rectangle.

这几个值决定了这一个alignment rectangle 的核心地点。

(4)Baseline:This value is the same as the bottom attribute for most, but not all, views.

那一个值日常和bottom attribute 相近,但不总是。

For example, UITextField defines its baseline to be the bottom of the text it displays rather than the bottom of the alignment rectangle.

比方,UIText菲尔德 定义它的baseline 在text 的底层,并不是在alignment rectangle的底层。

This keeps "descenders" (letters like 'g' and 'p' that descend below the baseline) from being obscured by a view right below the text field.

(5) Leading/Trailing:These values are used with text-based views like UITextField and UILabel.

这几个值用在像UITextField 和UILabel 的text-based views中。

If the language of the device is set to a language that reads left-to-right (e.g., English),then the leading attribute is the same as the left attribute and the trailing attribute is the same as the right attribute.

假若三个设备的语言设置为从左到右,那么leading attribute 和left attribute 相像,trailing attribute 和right attribute 相近。

If the language reads right-to-left (e.g., Arabic), then the leading attribute is on the right and the trailing attribute is on the left.

如果多个言语从右到左,那么它的leading  attribute 是在左手,而trailing attribute 是在左边。

By default, every view in a XIB file has an alignment rectangle, and every view hierarchy uses Auto Layout.

私下认可情状下,每一个在XIB中的view  皆有四个alignment rectangle,每一种view 都用auto  layout.

You do not define a view's alignment rectangle directly. You do not have enough information (screen size!) to do that.

您无法一贯定义一个view的alignment rectangle。你并不曾丰富的音信(荧屏的尺寸)做那么些。 

Instead, you provide a set of constraints. Taken together, these constraints allow the system to determine the layout attributes, and thus the alignment rectangle, for each view in the view hierarchy.

取代他的是您提供了一密密麻麻的constraints。 这几个constraints 放在一块儿,将同意系统调节layout attributes,因而也可能有为在view hierarchy中各种view的alignment rectangle  。

 

  1. constrants 

A constraint defines a specific relationship in a view hierarchy that can be used to determine a layout attribute for one or more views.

constranit定义了view hierarchy的叁个独出心裁的涉嫌。它亦可用来为二个或七个view决定一个layout attribute 属性。

For example, you might add a constraint like "the vertical space between these two views should always be 8 points" or "these views must always have the same width."

诸如:你能够加上那样叁个限量: "那八个view的垂直空隙应该总是8 points"也许"那些views 总是要求黄金时代致的增长幅度。"

A constraint can also be used to give a view a fixed size, like "this view's height should always be 44 points."

你也得以定义那样的:"那一个view 的万丈应该长久是44points"。

You do not need to have a constraint for every layout attribute.

你无需为种种layout 属性都设置叁个节制。

Some values may come directly from a constraint; others will be computed by the values of related layout attributes.

某个值从一个constraint 直接提交,而除此以外一些也许因此相关的layout attributes 总括取得。

For example, if a view's constraints set its left edge and its width, then the right edge is already determined (left edge + width = right edge, always).

After all of the constraints have been considered, there is still an ambiguous or missing value for a layout attribute, then there will be errors and warnings from Auto Layout and your interface will not look as you expect on all devices.

在有着的constraints 已经被构思了,不过仍然有一个歪曲或错失的值为叁个layout attribute ,那么将会有全自动构造或分界面错误或警告。 

Debugging these problems is important。

 

Let's see how using the view in the BNRDetailViewController's view hierarchy that will be simple to constrain – the toolbar.

让我们看看BN大切诺基DetailViewController's 的view hierarchy,来界定toolbar。

describe what you want the view to look like regardless of screen size. For the toolbar, you could describe it like this:

(1)The toolbar should sit at the bottom of the screen. 在底部
(2)The toolbar should be as wide as the screen. 和荧屏同样宽
(3)The toolbar's height should be 44 points. (This is Apple's standard for instances of UIToolbar.卡塔尔(قطر‎高度应该是44

To turn this description into constraints in Interface Builder, it will help to understand how to find a view's nearest neighbor.

把那几个描述转化成Interface Builder,它将帮扶您明白什么找到三个view 的近年的neighbor。

The nearest neighbor is the closest sibling view in the specified direction (Figure 15.6).

多个多年来的neighbor 是前段时间的sibling view 在钦定的倾向上。

图片 4

If a view does not have any siblings in the specified direction, then the nearest neighbor is its superview, also known as its container.

生龙活虎经二个view在钦命的大方向上sibling,那么它的nearest neighbor 是它的sup view,相当于它的container。

 

Now you can spell out the constraints for the toolbar:

  1. The toolbar's bottom edge should be 0 points away from its nearest neighbor (which is its container – the view of the BNRDetailViewController). 

  2. The toolbar's left edge should be 0 points away from its nearest neighbor.

  3. The toolbar's right edge should be 0 points away from its nearest neighbor. 

  4. The toolbar's height should be 44 points. 

Constraints can be added using Interface Builder or in code.

Constraints能够由此Interface Builder或代码加多上。

  1. Adding Constraints in Interface Builder

At the bottom righthand corner of the canvas, find the Auto Layout

constraint menu

图片 5

To turn these values into constraints, click the orange struts separating the values from the square in the middle. The struts will become solid lines.

图片 6

Find the Constraints section and reveal its contents. However, you will see only three constraints here. The fourth constraint, the fixed height of the toolbar, is in a separate Constraints section underneath Toolbar.

图片 7

 

Find the Constraints section and reveal its contents. However, you will see only three constraints here. The fourth constraint, the fixed height of the toolbar, is in a separate Constraints section underneath Toolbar.

 

Why the division? Once a constraint is created, it is added to a particular view object in the view hierarchy.

怎会好似此的界别?意气风发旦constraint 被创制,它被加多到在多个view hierarchy 的三个特意的view object中。

Which view gets a constraint is based on which views that constraint affects.

哪三个view获得这几个范围注重于constraint 影响了哪些view。

The three edge constraints are added to the Control because they apply to both the toolbar and its superview, the view of the BNRDetailViewController.

 

The height constraint, on the other hand, is added to the toolbar because it applies only to the toolbar.

If you select any of these constraints in the dock, a blue line will appear on the canvas representing the constraint. (Some constraints will be harder to see than others.) Selecting the view will show you all the constraints influencing that view.

5.Adding more constraints

 

Notice that the lines representing the constraints on the text field are orange instead of blue.

This color difference means that the text field does not have enough constraints for Auto Layout to unambiguously specify its alignment rectangle.

少年老成经这些颜色各异代表text 田野先生 未有丰盛的约束来结构来指明它的alignment rectangle。

图片 8

 

You can align two or more views according to any layout attribute. Here the best choice is to align baselines.

你能够align多少个或多个view依据其余贰个layout attribute.

On the canvas, select the text field and hold the Shift key down to select the Name label at the same time.

在canvas,选择text field 并按住shift 键选择Name label ,同时。

From the constraints menu, click the 图片 9icon to reveal the Align menu. Check the box next to

Baselines and add 1 constraint.

图片 10

 

You can also add constraints by Control-dragging on the canvas.

This dragging is similar to setting up outlets and actions. You drag from one view to another.

你的drag 从一个view到另一个。

After you release the mouse button, you get a list of constraints that you can add.

当您释放mouse button,你获取了一列你增加的范围。

 

Control-drag to the Name label again. This time, select Left from the menu. This is identical to opening the Align menu and checking Leading Edges.

 

Finally, you need to fix the label's height and width at their current values. Because these constraints affect only the Serial label, you do not Control-drag to another view. Instead, make a very short diagonal Control-drag within the Serial label.

图片 11

  1. Priorities 

Each constraint has a priority level that is used to determine which constraint wins when more than one constraint conflicts.

各个constraint 都有三个优先级,当多余二个约束的矛盾爆发时,决定哪些constraint 胜利。

A priority is a value from 1 to 1000, where 1000 is a required constraint. By default, constraints are required, so all of the constraints that you have added are required. This means that the priority level would not help if you had conflicting constraints.

贰个先行级是从1到1000的数值,而1000是要求的constraint.暗中同意意况下,限定是必得的。所以具有你的constraints is required .这也就表示priority level 无法援救您。

Instead, Auto Layout would report an issue regarding unsatisfiable constraints.

auto layout 假诺蒙受不顺心的constraints 就能告知三个标题。

Typically, you find the constraints that conflict and then either remove one or reduce the priority level of a constraint to resolve the conflict but keep all the constraints in play.

 

相像情况下,你会找到那些冲突,然后删除二个,恐怕下落多个constraint 的优先级,但保留全体,来化解。

7Debugging Constraints

7.1 Ambiguous layout

An ambiguous layout occurs when there is more then one way to fulfill a set of constraints. Typically, this means that you are missing at least one constraint.

叁个ambiguous layout 爆发当多于意气风发种方式填充限定群集。日常的,那就表示你足足缺点和失误二个constraint.

 

Add two labels to the UIControl under the dateLabel and position them next to one another.

在UIControl中加多多少个labels.

In the attributes inspector, change the background color of the labels to light gray so that you can see their frames.

把他们的背景颜色设置为light gray.

 

Now let's add some constraints to both labels. Hold down the Shift key and select both labels. Open the Pin Auto Layout menu, select the top, left, and right struts at the top, and then click Add 5 Constraints

图片 12

These labels do not have enough constraints to unambiguously define their frames.

那么些labels 没有充足的constraints 来显著地定义他们的frame。

Auto Layout takes its best guess at runtime, and it is not what you wanted on the iPad.

Auto Layout 在运作时使用最棒的guess,然则那实际不是您想要的。

You are going to use two UIView methods, hasAmbiguousLayout and exerciseAmbiguousLayout, to debug this situation.

您要求三个方法:hasAmbiguousLayout和exerciseAmbiguousLayout来debug那一个情形。

 

Open BNRDetailViewController.m. Override the method viewDidLayoutSubviews to check if any of its subviews has an ambiguous layout.

- (void)viewDidLayoutSubviews

{
for (UIView *subview in self.view.subviews) {

if([subview hasAmbiguousLayout]) NSLog(@"AMBIGUOUS: %@", subview);

} }

 

间接有个问号:在view controller里面的view的法子就可以写在View Controller里面吗?

 

viewDidLayoutSubviews gets called any time the view changes in size (for example, when the device is rotated) or when it is first presented on the screen.

当View 改革尺寸或它的第三次在显示器上现身时viewDidLayoutSubviews就能够被调用。

You can go a step further to actually see the other way this layout might appear. In BNRDetailViewController.m, edit the backgroundTapped: method to send the message exerciseAmbiguityInLayout to any ambiguous views.

你可以看见另二个layout。编辑backgroundTapped发送这一个新闻:exerciseAmbiguityInLayout到别的模糊的views。

- (IBAction)backgroundTapped:(id)sender

{
[self.view endEditing:YES];

for (UIView *subview in self.view.subviews) { if ([subview hasAmbiguousLayout]) {

[subview exerciseAmbiguityInLayout]; }

} }

 

Neither of the widths of the labels has been constrained, and so there is more than one solution to the system of Auto Layout equations. Because of this, there is an ambiguous layout and tapping the background switches between the two possible solutions.

 

Due to the other constraints you have specified, as long as one of the labels has its width constrained, the other label's width can be determined. You will get rid of the ambiguous layout by giving the two labels equal widths.

要是在那之中多少个label有二个固定的肥瘦,你就能够明确了。你就能够去除掉ambiguous layout。

图片 13

图片 14

那三个效能是风度翩翩致的。

 

In BNRDetailViewController.xib, Control-drag from one label to the other, and then select Equal Widths. Build and run the application on iPad.

 

The exerciseAmbiguityInLayout method is purely a debugging tool that allows Auto Layout to show you where your layouts could potentially end up. You should never leave this code in an application that you are shipping.

exerciseAmbiguityInLayout方法纯粹就是debugging方法,允许你看见您的构造潜在的表现格局。在职业代码中,差异意其冒出。

7.2 Unsatisfiable constraints 不充足的限量

The problem of unsatisfiable constraints occurs when two or more constraints conflict. This often means that a view has too many constraints.

当八个或多少个constraints conflict 就能够晤世unsatisfiable constraints .

 

. The label may look just as it did before, but take a look at the console. 

看一下现行反革命的调整台:

 

 

 Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(

    "<NSLayoutConstraint:0x10926e650 H:[UILabel:0x10926e3e0(280)]>",

    "<NSLayoutConstraint:0x109579870 H:|-(20)-[UILabel:0x10926e3e0]   (Names: '|':UIControl:0x10926cc40 )>",

    "<NSLayoutConstraint:0x109579910 H:[UILabel:0x10926e3e0]-(20)-|   (Names: '|':UIControl:0x10926cc40 )>",

    "<NSAutoresizingMaskLayoutConstraint:0x1092870b0 h=-&- v=-&- UIControl:0x10926cc40.width == _UIParallaxDimmingView:0x10957de70.width>",

    "<NSAutoresizingMaskLayoutConstraint:0x109287bf0 h=--& v=--& H:[_UIParallaxDimmingView:0x10957de70(768)]>"

)

 

Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0x10926e650 H:[UILabel:0x10926e3e0(280)]>

 

Break on objc_exception_throw to catch this in the debugger.

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

 

 

First, Xcode informs you that it is "unable to simultaneously satisfy constraints" and gives you some hints on what to look for.

先是Xcode 布告你"无法同一时候满意节制"

The console then lists all of the constraints that are related to the issue.

console紧接着列出了与此相关的具备constraints .

Finally, you are told that one of the constraints will be ignored so that the label will have a valid frame.

最后你应诉知那一个constraints中的一个将被忽略所以label 将会有二个使得的frame。

You can also just think through the problem. You constrained this label's leading and trailing edges to resize with the superview. Then you constrained its width to a fixed value. These are conflicting constraints, and the solution is to remove one.

 

delete the width constraint that you just added to the label and set its background color back to clear.

7.3Misplaced views 任意停放的view

If a view's frame in a XIB does not match its constraints, then you have a misplaced view problem.

尽管三个view 的frame 并不相配它的constraints,那么你将会有misplaced view 难点。

This means that the frame of that view at runtime will not match how it currently appears on the canvas.

约等于说这一个view的frame在启动时并不和它未来呈今后canvas中的相通。

Select the label that displays the date and drag it down a little bit so that the interface looks like

图片 15

A rectangle with an orange, dashed border will appear where the label used to be.

二个orangle ,虚线的矩形将晤面世在label原本的职分。

This is the runtime frame; at runtime, the existing constraints will position the label where this rectangle is and not where you just dragged it to.

那便是runtime frame,在runtime,已经存在的constraints 将会把label放置到orange矩形的职位,并非你适逢其会拖动的职位。

How you fix the problem depends on whether the view's size and position on the canvas are what you want.

您怎么修复那些难题在于view的size和position在canvas是不是是你想要的。

If so, then you change the constraints to work with this new position. If not, then you change the view's size or position to match the constraints.

若果是你想要的,你就改变constraints .假若不是,你能够校订view 的size或position 来协作这几个constraints.

Let's say that moving the label was an accident and that you want the view's position to match the existing constraints.

小编们假若label 的拖动是个意料之外,你要求view的position来协作已经部分节制。

Select the date label. Then, in the Auto Layout constraints menu, select the 图片 16icon to reveal the Resolve Auto Layout Issues menu

选料来缓和auto layout issues.

图片 17

Select Update Frames at the top. This will reposition the label to match its constraints.

选择update frame ,将会把重新放置label来合作它的constraints.

On the other hand, if you wanted the constraints to change to match the new position of the view, you

would choose to Update Constraints.

生机勃勃边假使您想让constraints 来协作已部分地点,你能够选择

(1)Update Frames:Adjusts the frame of the view to match its constraints.

(2)Update Constraints:Adjusts the constraints of the view to match its frame.

(3)Add Missing Constraints

:For views with an ambiguous layout, this will add the necessary constraints to remove the ambiguity. However, the new constraints might not be what you want, so make sure to double-check and test this resolution.

对于贰个ambiguous layout ,它能够加多要求的constraints。但是新的constraints 也许不是你想要得。所以自然要再检查一下。

(4)Reset to Suggested Constraints 重新设置被建议的constraints:This will remove any existing constraints from the view and add new constraints. These suggested constraints are sensitive to the context of the view. For example, if the view is near the top of its superview,the suggested constraints will probably pin it to the top, whereas if the view is near the bottom of its superview, it will probably be pinned to the bottom.

(5)Clear Constraints:  All constraints are removed. If no explicit constraints are added to this view, it will have the default fixed position and size constraints added to it.

抱有的constrains 将被移除。若无分明性地约束增加到这么些view上,它将会有暗许的position 和size constraints 增加上去。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

 

 

 

 

 

 

 

Programming Introduction to Auto Layout 自动构造,iosprogramming iOS Programming Introduction to Auto Layout   自动结构 A single application that runs natively on bot...

本文由金沙澳门官网送注册58发布于关于计算机,转载请注明出处:机动构造

关键词: