Search code examples
iosinterface-builderautoresizingmask

Fixed space between objects in both orientations


I am using the AutoresizingMask to define relative positions programatically. I got almost everything working but I cannot figure out how to preserve the same separation between two elements (UIButton, UILabel are separated by 20px) This is what I have:

UIButton, positioned at (100, 100), size: 60x30, autoresizingMask=UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin

UILabel, positioned at (180,100), size: 50x50, autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin

My problem is that I cannot maintain that 20px margin between these two elements when the orientation changes.


Solution

  • I've often found that I need to do a little more "adjustments" to my UI when changing orientations than what happens automatically.

    You can add this routine to your controller, and fine-tune your UI the way you like:

    - (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) o {
       -- Set the label to be 20px to the right of your button here
    }