Gallery Example
The screen has four identical rectangles separated by each rectangle with a label and an image view. Create a gallery project. In Main.storyboard , drag a view size of 284 by points.
Note: There is both main reasons why do you would drop a plain UIView onto a storyboard:
A) you ' re going-to-use it as a container-which helps with organizing the content of your scenes; Or
b) It is a placeholder for a custom view or control, and you'll also set its Class attribute to the name of your own uiview or Uicontrol subclass.
Now set some constraints. You already know two kinds of constraints, using the Editor\pin and align menus. or ctrl+dragging control to implement. Then there is another way to use the tool options below.
Align, Pin, Resolve Auto layout issues, and resizing behavior from left to right are aligned, fixed, solve Auto layout problems, redefine size.
The top three options are the same in the menu section, resizing behavior allows you to change the constraint when resize view
Select a pin to eject the basket body as above. Then click on the four T-bar colors to turn solid red, and finally choose the Add 4 constrains option to create a new constraint.
Select Priview in Assistant editor to preview the emulator vertical and horizontal screen
Note: Maybe you wondered what the constraint at the top of the "The view didn ' t go all the" the "the" the "the" the "top" of the screen:
Instead it stops at the status bar. But in IOS 7 the status bar was always drawn on top of the view controller-it was no longer a separate bar-so what gives ? When your created the constraint it didn ' t actually attach to the top of the screens but to a invisible line called the top LayoutGuide.
On a regular view controller This guide sits @ points from the top of the "screen" at least when the status bar was not Hidden. In a navigation controller it sits below the navigation bar. Because the navigation bar has a different height in landscape and the Top Layout Guide moves with the bar when the device is Rotated. That's makes it easy-to-place views relative to the navigation bar. There is also a Bottom Layout guide, that's used for the tab bar and toolbars.
In order not to change the width and height of the simulator after rotation, you can set the width and height
Run app, report error
2015-01-04 10:25:58.691 gallery[888:279956] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list are one you don ' t want. Try this: (1) Look at the 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 so you don ' t understand, refer to the documentation for the UIView Property translatesautoresizingmaskintoconstraints)
(
"<nslayoutconstraint:0x7f8adae04f50 h:[uiview:0x7f8adac0b950 (]>)",
"<nslayoutconstraint:0x7f8adae08360 uiview:0x7f8adac0b950.leading = = Uiview:0x7f8adae05d80.leadingmargin + 204 > ",
"<nslayoutconstraint:0x7f8adae083b0 Uiview:0x7f8adae05d80.trailingmargin = = uiview:0x7f8adac0b950.trailing + 204 > ",
"<nslayoutconstraint:0x7f8adeb6c870 ' Uiview-encapsulated-layout-width ' h:[uiview:0x7f8adae05d80 (375)]>"
)
Would attempt to recover by breaking constraint
<nslayoutconstraint:0x7f8adae04f50 h:[uiview:0x7f8adac0b950 (]>)
Make a symbolic breakpoint at uiviewalertforunsatisfiableconstraints to catch this in the debugger.
The methods in the Uiconstraintbasedlayoutdebugging category on UIView listed in <UIKit/UIView.h> could also be helpfu L.
2015-01-04 10:25:58.693 gallery[888:279956] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list are one you don ' t want. Try this: (1) Look at the 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 so you don ' t understand, refer to the documentation for the UIView Property translatesautoresizingmaskintoconstraints)
(
"<nslayoutconstraint:0x7f8adae08360 uiview:0x7f8adac0b950.leading = = Uiview:0x7f8adae05d80.leadingmargin + 204 > ",
"<nslayoutconstraint:0x7f8adae083b0 Uiview:0x7f8adae05d80.trailingmargin = = uiview:0x7f8adac0b950.trailing + 204 > ",
"<nslayoutconstraint:0x7f8adeb6c870 ' Uiview-encapsulated-layout-width ' h:[uiview:0x7f8adae05d80 (375)]>"
)
Would attempt to recover by breaking constraint
<nslayoutconstraint:0x7f8adae083b0 Uiview:0x7f8adae05d80.trailingmargin = = uiview:0x7f8adac0b950.trailing + 204 >
Make a symbolic breakpoint at uiviewalertforunsatisfiableconstraints to catch this in the debugger.
The methods in the Uiconstraintbasedlayoutdebugging category on UIView listed in <UIKit/UIView.h> could also be helpfu L.
2015-01-04 10:25:58.693 gallery[888:279956] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list are one you don ' t want. Try this: (1) Look at the 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 so you don ' t understand, refer to the documentation for the UIView Property translatesautoresizingmaskintoconstraints)
(
"<nslayoutconstraint:0x7f8adae05be0 v:[uiview:0x7f8adac0b950 (284)]>",
"<nslayoutconstraint:0x7f8adae08400 v:[_uilayoutguide:0x7f8adae06db0]-(Bayi)-[uiview:0x7f8adac0b950]>",
"<nslayoutconstraint:0x7f8adae08450 v:[uiview:0x7f8adac0b950]-(215)-[_uilayoutguide:0x7f8adae07ca0]>",
"<_uilayoutsupportconstraint:0x7f8adae080f0 v:[_uilayoutguide:0x7f8adae06db0 (]>)",
"<_uilayoutsupportconstraint:0x7f8adae08290 v:|-(0)-[_uilayoutguide:0x7f8adae06db0] (Names: ' | ': UIView:0 X7F8ADAE05D80) > ",
"<_uilayoutsupportconstraint:0x7f8adae08ab0 v:[_uilayoutguide:0x7f8adae07ca0 (0)]>",
"<_uilayoutsupportconstraint:0x7f8adae08150 _uilayoutguide:0x7f8adae07ca0.bottom = = UIView:0 X7f8adae05d80.bottom> ",
"<nslayoutconstraint:0x7f8adeb6c8c0 ' Uiview-encapsulated-layout-height ' v:[uiview:0x7f8adae05d80 (667)]>"
)
Would attempt to recover by breaking constraint
<NSLAYOUTCONSTRAINT:0X7F8ADAE05BE0 v:[uiview:0x7f8adac0b950 (284)]>
Make a symbolic breakpoint at uiviewalertforunsatisfiableconstraints to catch this in the debugger.
The methods in the Uiconstraintbasedlayoutdebugging category on UIView listed in <UIKit/UIView.h> could also be helpfu L.
Remember what you said before you had to set enough constraints to make auto layout know how to lay out? And now the situation is that there are too many constraints set. Where there is a conflict. Once the error "unable to simultaneously satisfy constraints" means that you have set too many constraints to conflict. Let's see what's going on here.
You can see that the screen has 6 constraints set, four previous constraints 1-4 and then there are new width and height constraints to view 5 and 6. So where is the conflict? There should be no problem in the upright mode because of the mathematical operation. The width of the superview is the points. If you add a horizontal distance length and width constraint, then you should end up getting 320. The calculation principle is 98+160+62 = 320 (that is, the width of the left-hand basket to the left border of the view +view the width of the +view right-hand basket to the right of the Superview) the same vertical constraint should be 568. But when you rotate the device to a horizontal screen, the window is 568 points width. It means 98+160+62+? = 568. There are 248 additional points auto layout does not know how to obtain.
The conflict is that either the width of the view is determined and one of the margins must be variable or margins determined and width variable. You can't set their values to death at the same time. So one of the constraints needs to be removed. In the example above, you want the width and height of the view to remain constant during rotation, so the trailing horizontal distance must be removed. Delete the horizontal space on the right and the vertical space at the bottom.
Running the app again is fine.
Painting the Portraits
Add a new label to the view and you can notice that guides appears as it will be the label of the Superview
Set the label's constraint to center the layout horizontally
Drag Image View in
1. Drag the image view into the green view but don ' t worry too much on its size or position:
2. With the image view selected, press the Pin button and choose the following options:
Be sure to select the Items of New Constraints to the update frames option, not the default, otherwise auto layout will appear as follows, Because the frame you set is in conflict with the default ImageView frame, it is not correct.
Of course you can also use the following method to modify this effect Resolve Auto Layout issues
Download The resources for this tutorial download the picture resource file, set the ray.png to set the background to white and set the picture to fit in a Aspect fit
You can see that the constraints in green view become orange. This problem occurs when you set the image view. So how did your layout suddenly fail? Fortunately, Xcode can tell you what's wrong.
There is a content priority ambiguity error. This means: Image view and label do not have a fixed height. Auto layout does not know how much the scale is if green view changes height. (Interface Builder seems to overlook the point that green view actually has a fixed height constraint)
For example, your green view is taller than points. So how does auto layout allocate this new points between the label and the image view? When label or previous size image view will increase by points? Or does the image view or the previous size label increase by points? Do they all have extra points or will he divide it into 25/75, 40/60, or other possible divisions?
If you do not solve the problem then auto layout must guess and the result may be unexpected.
The right solution is to change the label's content compression resistance priority. Select the size of the label Inspector settings vertical content Compression resistance priority is 751. So the label's priority will be higher than the image view. Set content hugging priority to 252. These two numbers are the suggested numbers that are displayed when an error is made. ImageView's default Night Market is 750,251, which is higher than ImageView.
There's no warning.
Adding the other heads
Drag green view to the top left corner. Use Resolve Auto Layout issues Update constraints.delete that "Vertical Space ( -20)" Constraint
Choose ? D to copy one of the same and move to the upper right corner, with Orange, set top and trailing to Yes
Duplicate the same two pin to bottom left and bottom right
But there's a problem with the horizontal screen.
This is because you set a fixed width and height, so they are always this height and width without changing the height and width of the superview.
Select Width and Height (284) and delete, and the run will look like this:
Note: If you ' re wondering why some of the "views" is larger than others, this is again related to the intrinsic content size. The size of the image determines how large the image view is; The size of the text determines how large the label is. Taken together with the constraints for the margins-20 points on all sides-this determines the total size of each view .
Use in document line ? Select all the color view, you can add all the constraints at once. Select equal widths and equal heights in the PIN and select Add 6 constraints. It's not right to run again, but you'll find all the view widths and heights are the same.
This is because there is no constraint between them, and auto layout doesn't know how to lay them out.
Select Ray and Matthijs and select Pin\horizontal Spacing. Because they are side by side, this adds a constraint between their horizontal space 0 and is enough for auto Layout knows that the two view (s) are related. and add a editor\pin\vertical Spacingbetween Ray and Dennis.
Run the app again, and this time it looks all right:
Beginning Auto Layout Tutorial in IOS 7:part 6