Preface
AutoLayout is very powerful and easy to use, and is also very readable. With various third-party AutoLayout libraries, you can set up a game like a dog that loses its chain! However, there are various problems in use, so let me introduce them in detail below.
AutoLayout Problem
- Layout conflict
- Intrinsic dimensional conflict
- Layout and inherent dimensions conflict
Main concepts
- IntrinsicContentSize (meaning I know my size, if you don't specify a size for me, I will follow this size.)
- hugging,compress
- priority
- constraint:equal,unequal
A UIView display requires 2 elements, position & size. AutoLayout can determine these two view , so does not use frame.
1. Problems encountered in layout
Absolute conflict in layout:
eg sets the left and right of the view, and then there is another width, causing conflict
Solution:
1. Set priority (the system ignores the system by default when the low priority constraint is not used)
2. Delete the constraint that does not have (commonly used)
2. Layout fuzzy conflict
Assume that the width of UIView2 cannot exceed 50. When the width of UIView1 is less than 50, the two are equally wide;
When the width of UIView1 is greater than 50, UIView2 is not affected by the width of UIView1.
So add a constraint to UIView2: width <=50. At this time the conflict came:
Because the width of UIView1 is set, while UIView2 and UIView1 are equal in width. Then the width of UIView2 is determined.
so When the width is > 50, it is a conflict, and when <= 50, it is not a conflict
Solution:
Set the equal width priority lower so that View2 first satisfies the constraint of <= 50, and when its width <= 50, equal width takes effect
enum { UILayoutPriorityRequired = 1000, UILayoutPriorityDefaultHigh = 750, UILayoutPriorityDefaultLow = 250, UILayoutPriorityFittingSizeLevel = 50, };
1. Intrinsic conflict
content Hugging/content Compression Resistance Set priority to resolve intrinsicContentSize
- Hugging (Don't want to fill space)
- Compression (Don't want to be squeezed)
- The view with these two characteristics must implement intrinsicContentSize
- Hugging default priority = 250
- Compression default priority = 750
- Other constraints default priority = 1000 (maximum)
When > View will be stretched
When < View will be held tightly on the smaller side of priority
When > View will be squeezed
When < View will not be squeezed
eg UILabel has intrinsicContentSize, then use it for the size of UILabel.
If there is a constraint set size, then use constraint
UILabel1, and UILabel2 layout:
It is 50 o'clock away from the upper sidebar. The distance between UILabel1 and the left bar is 10, and the distance between UILabel2 on the left is 10 points.
Because they all have Intrinsic attributes, there is no need to specify the size, and the location should be clear.
Add a constraint to UILabel2, and the right side is 10 points away from the right side. (If the view does not have an intrinsicContentSize, then the view on the right will be stretched)
Because UILabel has intrinsicContentSize, one of the two Labes must be stretched
* 1 label uses its own intrinsic, and the other fills other spaces
Use scenarios
1. View without intrinsicContentSize uses priority to change fuzzy conflicts into conditional layout
2. View with intrinsicContentSize
Change the priority of hugging or compression and resolve intrinsic conflicts
Although there is intrinsic, just give origin and size, use unequal constraint to improve the layout and avoid warnings
Main interface & properties
UILabel preferredMaxLayoutWidth, if there are multiple lines to set the correct preferredMaxLayoutWidth, to get the correct intrinsicContentSize
systemLayoutSizeFittingSize Call intrinsicContentSize
layoutIfNeed Force refresh if there is layout change
When the inner content of the view changes, invalidateIntrinsicContentSize needs to be called to let the system know to recalculate the intrinsicContentSize the next time it is laid out.
// The original view size does not match the current one.- (void)layoutSubviews { [super layoutSubviews]; _collectionView.frame = ; if (!CGSizeEqualToSize(, [self intrinsicContentSize])) { [self invalidateIntrinsicContentSize]; } }
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.