Autoresizing masks programmatically vs Interface Builder / xib / nib

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.

Leave a Comment