Today, my friend sent me a message to help me write a demo, as follows:
This is actually a typical four sub-layout, at the same time in order to ensure that the circle picture is not deformed and the distance between up and down is 10, in order to achieve this effect, my solution is as follows:
@: To ensure that the picture is not deformed, only need to ensure that after the constraint, its width and height of the proportional relationship is 1:1.
To achieve the image is not deformed, to ensure that the proportional relationship between the picture is one way, and the other way is often used to set the image of the Contentmodel property for aspect Fit. If you set its properties to aspect fit, you will not be able to meet the distance between the top and bottom of the 10. So in order to achieve this effect, I first use four view to achieve four equal, and ensure that the ratio of the view is 1:1. This adds a uiimageview to each view, with the upper and lower constraints, with a value of 10. The above effect is achieved in such a way.
Let's start by creating a view,view distance from the bottom, left, and right is 0 and the height is 120.
Then we add four view to view, and set four view and so on high width,
When this step is complete, we add the upper, lower, and left constraints to the View1, with a value of 0. Add upper and lower constraints to View2, VIEW3, and values are 0. Add upper, lower, and right constraints to view4, with values of 0,
View2, VIEW3, View4 is the same truth, here is not repeating.
In the Xcode7, found a new phenomenon, elected in View1 and View2, in the selection editor, will find no pin this option, you can not find the horizontally space is often used to this constraint. In fact, the Apple did not remove this attribute, it is hidden here: we first select View2, and then hold down the control key, so that it points to View1 will see the property.
and modify the value of horizontal spacing to 0 (that is, modify leading Space to:). The spacing between View1 and View2 is set to 0.
The same method is used to set the spacing between View2 and VIEW3, VIEW3 and view4 to 0. At this point, we have achieved the layout of four equal portions, and there are no red error prompts. To make it look more distinguishable, we set a different background color for the four view, and the final effect is as follows:
we've already implemented a four-percent layout, and the next step is to achieve a four-view proportional relationship of 1:1. This is also very well implemented, check View1, set its aspect ration value is 1:1, because the previous constraint added four view is equal width, so the remaining three view of the proportional relationship also becomes 1:1. such as:
this time if we look at its proportional relationship is not 1:1, let it change to 1:1. such as:
When we have completed this step, we find that the layout has been wrong and the error is as follows:
Why is there such a mistake? The layout has actually been conflicted because the constraints added to View1, View2, VIEW3, and view4 are the same height as their parent view (parent view view). For parent view views, when we add constraints, they are fixed at a height of 120. At this point, we add a proportional relationship of 1:1 to View1, whose height changes with the width. On the 3.5 screen, the 4.0 screen whose width and height is 80, the 4.7 screen width and height is 94, and the 5.5 screen width height is 103. Because of the existence of two constraints on its height constraints, and two priority is the same, the system does not know which to choose for the layout calculation, so there is a constraint conflict. To resolve this layout conflict, you only need to lower one of the priority levels. Obviously, we want to change the priority of the view height of parent views and adjust the height to >=50. This will not result in a constraint conflict, because the proportional relationship takes precedence over the >= priority level. Also, why change it to >=50 instead of >=120? This is because if set to >=120. The maximum height on the phone screen is 103, which is smaller than 120, which causes the view1 height or 120, not the 103 we want. To ensure that the proportional relationship is 1:1, we take a value smaller than 80. This step is completed as follows:
through, we can see that there is no error message. The next step is to add Uiimageview to the four view. Add the upper, lower, left, and right constraints to the Uiimageview, with values of 10, such as:
At this point, we have achieved a four sub-layout and the proportional relationship is 1:1, the last of which is as follows:
3.5-screen:
4.0-screen:
4.7 On-screen:
5.5 On-screen:
Demo Link Address: https://github.com/guoshimeihua/QuarteringDemo.git
IOS Auto-layout achieves four equal and wide-high ratio is 1:1