217

I was in an (probably false) assumption that enabling the right margin indicator in xib is equivalent to using UIViewAutoresizingFlexibleLeftMargin inside code and so on.

So, I used to think according to this snapshot: enter image description here

Later today I had to cross check, and stumbled upon this thread.

And also the apple documentation, entitled with the section with title - "Handling Layout Changes Automatically Using Autoresizing Rules" in this link: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html

So I now have a renewed concept in my mind as to how setting autoresizing masks programmatically would be equivalent to xib settings:

Scenario 1: Setting only (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) is equivalent to:

(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)

In XIB?

Scenario 2: Setting (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin) in code is equivalent to:

(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)

In XIB?

Are my 2 renewed scenarios correct? Am I right now in my understanding?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Raj Pawan Gumdal
  • 7,390
  • 10
  • 60
  • 92
  • 3
    You kidding me? But there seems to be where my confusion too. So if I wanna hug top I turn on the bottom autoresize. Good job apple. This is the most idiotic setup I have ever seen. – user4951 Oct 16 '12 at 06:27
  • 1
    If you want to hug top you have to make sure that you won't mention UIViewAutoresizingFlexibleTopMargin in the bit mask code. My earlier assumption was wrong and thats why I had posted this question to clear stuff out. – Raj Pawan Gumdal Oct 16 '12 at 08:52
  • Your assumption is correct. Where are you wrong? – user4951 Oct 16 '12 at 09:28
  • Yes, the two scenarios you cite are correct. –  Aug 03 '13 at 14:15
  • Only tangentially related, but a quick note for others stumbling onto this question that another factor in the resizing views "equation" is to the ContentMode property. FYI. – livingtech Feb 27 '14 at 15:58
  • 3
    I made a simple tool for this: http://erkanyildiz.me/lab/autoresizingmask you can use it. – erkanyildiz Jul 07 '16 at 07:29

4 Answers4

47

Yes, you have cited things correctly. Also, I agree that it feels a bit backwards, so for that reason I appreciate your post.

You might like using a preprocessor Macro UIViewAutoresizingFlexibleMargins when making a UIView's margin flexible in every direction. I put this in the precompiled header file so it gets included everywhere.

#define UIViewAutoresizingFlexibleMargins                 \
              UIViewAutoresizingFlexibleBottomMargin    | \
              UIViewAutoresizingFlexibleLeftMargin      | \
              UIViewAutoresizingFlexibleRightMargin     | \
              UIViewAutoresizingFlexibleTopMargin

Using UIViewAutoresizingFlexibleMargins will make a UI Element stay centered since it will NOT be hugging any one side. To make the element grow / shrink with its parent, set the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleHeight respectively.

I like using UIViewAutoresizingFlexibleMargins because I can later reference it like:

myView.autoresizingMask = UIViewAutoresizingFlexibleMargins;

instead of

myView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;

All to often I see these margins OR'ed together on one line like the example above. Just hard to read.

Sam
  • 26,946
  • 12
  • 75
  • 101
  • 4
    Flexible in every direction should be (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight) I guess? Or am I confused again! – Raj Pawan Gumdal Oct 19 '11 at 05:39
  • 4
    FlexibleMargins will make the element stay centered (not hugging the left, top, right, or bottom margin). Flexible width / height will make the ui element grow / shrink respectively. – Sam Oct 19 '11 at 17:50
  • 4
    Is everything clear now? Also, I really meant it when I said I appreciated your post. I actually favorited it b/c this has confused me in the past. I think it's a great Q. – Sam Oct 19 '11 at 19:31
  • 3
    Yes, thanks, now I get it, "Flexible margins" and "Flexible width / height". That explains it all, and your "not hugging the left, top, right, or bottom margin" is a good way of explaining :) – Raj Pawan Gumdal Oct 20 '11 at 10:17
35

Yes, Interface Builder has it "reversed" in a sense (or UIView, depending on how you look at it). Your cited "scenarios" are correct.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

enter image description here

Enabling the vertical/horizontal arrow (called spring) inside the box will make the height/width flexible. But enabling an outside line (called strut) will make that side inflexible/ non-flexible.

Enabling the outer left line (left strut) is not equivalent to enabling UIViewAutoresizingFlexibleRightMargin. Instead, UIViewAutoresizingFlexibleRightMargin = on if right strut disabled, off if right strut enabled.

It is quite confusing at first, but if you see closely, there is a difference in the springs and struts. I don't know why Apple did this, but for me, there were some cases where it was easier to use. And using opposite properties in code is even more confusing.

Abdurrahman Mubeen Ali
  • 1,331
  • 1
  • 13
  • 19
0

Swift 4 use this

gadBannerView?.autoresizingMask = [.flexibleRightMargin  , .flexibleLeftMargin , .flexibleTopMargin , .flexibleBottomMargin]

Objective-C

myView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
Muhammad Nayab
  • 1,612
  • 14
  • 14
  • your set is equal to [.flexibleRightMargin, .flexibleBottomMargin] what makes the frame x,y, width and height constant. X and Y in UIKit relates to top left corner. – Blazej SLEBODA Oct 23 '20 at 19:32