Search code examples
objective-ciosuiviewautoresizingmask

iOS: AutoresizingMask using an array?


Is it possible to provide an array to the autoresizingMask property of UIView? The reason I want to do this is that I have some conditions which determine which autoresizingMask properties I want to add to my my view.

So, instead of just using:

self.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;

I want to do something like:

if (addMargin) {
   [items addObject:UIViewAutoresizingFlexibleRightMargin];
}
if (addWidth) {
   [items addObject:UIViewAutoresizingFlexibleWidth];
}

// Add to property
self.view.autoresizingMask = items;

So I basically want to conditionally set the items of this property.


Solution

  • It's a bit mask. Just bitwise-OR it with the one you want.

    if(addMargin)
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleRightMargin;
    if(addWidth)
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleWidth;
    

    To reset the mask, you can set it to 0, or if you want to remove a particular attribute you can negate it and bitwise-AND the mask with it:

    if(removeMargin)
        self.view.autoresizingMask &= ~UIViewAutoresizingFlexibleRightMargin;