Review iOS diary automatic layout 08- [2020]

preamble

The main record for ui auto-layout

text

0. The code calculates the frame by
this stupid, but very free, to get through the screen width and height, automatic aspect ratio adjustment assembly code is used.
Nothing to talk about, like how to use how to use.

1.autoresizing automatically adjusts
its core idea is reference to the parent of the control frame, automatically adapting the size ratio
(parent control frame is now 100, is the child controls 20)
(if the parent control frame becomes 200, the child controls will become 40)
it has the disadvantage that it can only set the relative relationship between the current and the control parent control.

2.autolayout automatic layout
as autoresizing it can not guarantee the brothers pitch control. It can only be based on the parent container
but autolayout can have a very convenient auto layout settings.

3.size classes + autolayout
because Apple screen more and more, with autolayout can not fit the perfect
to be a perfect fit by using constraints.
It can be in different screens, different rules
such as: 3.5 inch screen want a button in the top, but I hope this button in 4.7inch display in the middle

autoresizing is an old technology, not recommended, and understand the line
should be the main learning autolayout, and autolayout and autoresizing are mutually exclusive. Only one.

Implementing stories by autoresizing:

The four corners of the screen, respectively, into four squares buttons, regardless of screen size change, consistent corners button.
1. Because autoresizing reference to the parent class, it must be placed in view inside a view
into the inside of the button in this view.
This when you start your phone is no problem, but the screen cross over, the four corners of the box that appears immediately there is a problem.

Use autoresizing, click on the right of the small ruler button
to find autoresizing options, a total of six sides (four sides which is actually two).
4 sides outside, that is, relative to the parent container.
Top of the line: from the parent control the distance is not fixed
(if not fixed, then the parent of the hypervariable large, this component is not going to follow the parent's shift)
of the left line: distance from the parent control is not fixed
(if not fixed , then the parent class width becomes large, this component is not follow) left parent class
at the right is the same token
, if not all selected, it will according to the ratio of the parent class, the automatic movement of
such parent 1 times larger, its coordinates will be larger doubled, but their size does not change.

Inside the two sides, whether the parent container as the width and height, increase automatically
if all selected, a larger parent class, a subclass can also become large
if becomes great, also we want relative distance, line disposed outside the line.

Code implementation autoresizing settings by
using Uiview of autoresizingMask set. Enumeration inside look at the code to open the head.
The main enumeration inside to the variable option, if a variable is set left, then right is immutable.
Want more settings with | symbol on it

Used autolayout realize Case

Mainly based on a control, reference or set of constraints.
Example 1 with reference to control the distance y 2 of the control 10, will then autolayout. Layout automatically in accordance with this setting.
Obviously this is impossible autoresizing demand, it can only be based on the parent control to automatically change the size of it

The first button is disposed aligned
leading edges left portion aligned
training edges right portion aligned
top edges aligned with the top of
bottom edges aligned with the bottom
Horizontal Centers
Vertical Centers
Baselines
Horizontal Center container in a parent container horizontally aligned
vertical center in container vessel vertically aligned parent

The second button set fixed frame
setting a fixed distance from the control of a control (it can choose to set different controls distance fixed)
width fixed width is always the same
height fixed height is always the same
equal widths and additional controls as wide
equal heights and Further, like the control of high
aspect ratio and proportion as additional controls
align alignment settings.

The third button layout automatically solve some of the problems secondary button
relations after resize child controls set the fourth button parent control

After setting may appear red arrow This constraint is problematic
1. just set the width and height constraints, but did not set a specific location
constraints of more than 2, after setting a high width, and set up a wide high.
after setting appears orange warning arrow
in accordance with the constraints we set, but the position of the button and our constraints are not the same
need only update the position down buttons on it. this is all right.
button controls of this type, even though do not set the size, also have default values.

Constraint codes by adding autolayout

If the control is bound between brothers, it is necessary to the most recent common parent element body
if it is a separate own constraints, you can add your own body.
A constraint is an object NSLayoutConstraint
corresponding space to get, use addConstraint
warning : before setting must prohibit autoresizing function NO
view.translatesAutoresizingMaskIntoConstraints = NO
and not set up to view frame

Object class methods initialize NSLayoutConstraint
what attribute object is equal to b x what attribute object
multiplied by constant multiplier plus

Based on the animated autolayout

After the tow line to get the properties, the changes that you want
, but remember, the implementation of the animation code uiview inside. It is useless
to extra method call layoutIfNeede

Published 15 original articles · won praise 0 · Views 2561

Guess you like

Origin blog.csdn.net/u014270781/article/details/105322469