Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of setting Constraints vs Setting Frames of UIViews

What are the advantages of setting constraints vs simply setting the frames of UIViews? What can a constraint do that simply setting the frame from based on UIScreen.MainScreen.Bounds or View.Bounds can't?

like image 734
LampShade Avatar asked Apr 15 '15 15:04

LampShade


2 Answers

Setting frames do not guarantee resizing when the frame changes (through rotation). Autolayout does guarantee this by adjusting the view based on frame and relative sizes. You would most likely use autolayout in a multi-orientation app.

like image 170
Schemetrical Avatar answered Nov 21 '22 22:11

Schemetrical


Setting constraints means you no longer have to think about frames. You no longer have to think about calculations for spacing and placing items next to each other, nor do you have to think about sizing on different screens.

All you have to actually think about is how you want your views to be placed in relation to one another or in relation to the screen. This greatly simplifies UI code, as it takes all the procedural placement out of the equation.

Simply put, AutoLayout is the best way to place views on screen and have everything just work. And it should be used in most cases.

like image 40
anon_dev1234 Avatar answered Nov 21 '22 22:11

anon_dev1234