Using iOS 6 Auto Layout to position view relative

2020-07-18 05:24发布

问题:

I wish to have objects positioned relative to the size of the superview instead of absolutely.

For example, I do not want to say that an element is "50px from the top", I want to say it is "1/3 of the way down".

Is there any way to do this using auto layout? If not, what is the best way?

Cheers

回答1:

Add an Outlet to the height constraint of superivew and an outlet constraint to the topspace. In code set the constant of topspace constraint to a value equal to 0.33 * height constraint of superview.



回答2:

If you use IB, just place the element where you want it and set it to maintain its Top Space to Superview. This will hold the element at that distance from the top for both 4 and 3.5 inch devices.