2

When using auto layout, I am confused on how to set the size of both my view and, more importantly, my labels/buttons/images.

I have read dozens of webpages perspectives. Many say that if I am using auto layout correctly, I should not need to explicitly state the size of my view objects. Yet, auto layout seems to almost demand exactly that, explicit size constraints! If I do not give them, auto layout says my image and view sizes are "ambiguous".

I would also like to know what auto layout will do when my conatraints are loaded on a retina display. Will my 300x90 image suddenly be half the size? Will a view I pin 70 from the top now be covered by a navigation bar? Most auto layout tutorials focus on how to use constraints, not how my 300x90 image will be manipulated by the iOS on different devices.

It seems like, if they wanted the view to be fluid then I should be specifying percents, not specific pixel measurements!

Can anyone help me better understand how my views will scale up and down?

Dave G
  • 12,042
  • 7
  • 57
  • 83

1 Answers1

1

I have read dozens of webpages perspectives. Many say that if I am using auto layout correctly, I should not need to explicitly state the size of my view objects. Yet, auto layout seems to almost demand exactly that, explicit size constraints! If I do not give them, auto layout says my image and view sizes are "ambiguous".

It is important to distinguish between normal views and scroll views here. For normal views, you can lay out your content and constrain your views to edges of the super view or to surrounding views and this works.

For scroll views, it is necessary for Auto Layout to be able to compute the size of the contentView. This can be done in one of 2 ways. You either 1) explicitly set the width and height of your contentView with constraints, in which case you can now treat the contentView as a normal view and lay it out accordingly OR 2) you size and constrain all of the objects in the contentView explicitly so that the size of the contentView can be computed from the sizes of the objects it contains. In other words, you either fix the size of the contentView so that AutoLayout can place the other items, or you fix the size of the items so that Auto Layout can compute the size of the contentView.

I would also like to know what auto layout will do when my conatraints are loaded on a retina display. Will my 300x90 image suddenly be half the size? Will a view I pin 70 from the top now be covered by a navigation bar? Most auto layout tutorials focus on how to use constraints, not how my 300x90 image will be manipulated by the iOS on different devices.

Constraints are done in points, not pixels. Your 300x90 image is 300 points by 90 points. It will be sized properly for all devices. If you provide additional image assets for @2X and @3X, it will even use the proper image for each device.

It seems like, if they wanted the view to be fluid then I should be specifying percents, not specific pixel measurements!

You can use the multiplier property of the constraints to achieve percents. For instance, if you want your image width to be 50% of the width of the super view, set an Equal Widths constraint between the image view and its super view and then change the multiplier to be 0.5.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • You resolved each of my confusions, including my other post on scroll views. Thank you. My auto layout views not sizing correctly was when dealing with scroll views, you were correct about that. I also did not know it was points not pixels, and I had read nothing abut the multiplier property. As a side note, I did read Apple's guide to Swift, watched lots of YouTube videos, and read quite a bit online. I don't mean to be lazy by asking others for help. If there's a book or resource I should read to be more self-sufficient, please let me know. – Dave G Mar 02 '15 at 15:35