6

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?

LampShade
  • 2,675
  • 5
  • 30
  • 60
  • 1
    You should watch the 2012 WWDC videos on auto layout (there are 3 of them). There are many many things you can do with auto layout that would be difficult and require a lot of code if you set frames. – rdelmar Apr 15 '15 at 16:00
  • Thanks for the suggestion, I definitely will. – LampShade Apr 15 '15 at 16:21

3 Answers3

3

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.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • When you say multi-orientation you mean an app that uses landscape and portrait? As opposed to portrait vs upside down for example? – LampShade Apr 15 '15 at 15:28
  • Yep. Portrait and upside down require no frame changes. – Schemetrical Apr 15 '15 at 15:30
  • 3
    I'm not convinced by your answer. I always wrote my frame code in layoutSubviews or viewWillLayoutSubviews anyway and wrote all the calculations as a relationship to everything else. So orientation changes, text size changes, all represented beautifully without using a single constraint. Easier to read than constraints too. – Mike S Oct 19 '16 at 14:02
3

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.

anon_dev1234
  • 2,143
  • 1
  • 17
  • 33
1

Using auto layout contraints, I never have to worry about setting frames or deal with frames in multiple screen sizes (there are 4 different sizes of just iPhones that run iOS 8).

Benefits to auto layout

  • adjusts automatically for different screen sizes
  • adjusts automatically for landscape to portrait (or vise versa) orientation changes
Community
  • 1
  • 1
Taylor M
  • 1,855
  • 1
  • 14
  • 20
  • say you have 4 labels, from top to bottom, how would you position these using autolayout. what I mean is you have 3 kinds of constraints: first label and top of view, between 2 labels, last label and bottom of view. How would I specify these constraints so that it not stretch out the spaces in between too much nor it would stretch out the height of the labels themselves? Would I give percentage-base spaces/distances/heights or just fixed amounts (which is different for iPhone5,6,plus, iPad mini, iPad Air, iPad Pro) – mfaani Mar 22 '17 at 17:38