appinator
appinator

Reputation: 3

auto layout rotation fix position

Hi i'm struggling with the auto layout in xcode5. Maybe I'm totally wrong and there is another way to get my desired result.

take a look:

this is my portrait view how i made it in Interface Designer

enter image description here

this is how i want the button to stay and rotate in landscape

enter image description here

So is there a way to accomplish this in interface builder and auto layout or is there another way to go?

Upvotes: 0

Views: 392

Answers (2)

Nikita Took
Nikita Took

Reputation: 4012

Yes, there is.

There is 2 ways: short and "right".

Short way

  1. Add notification in viewDidLoad (and don't forget to unsubscribe in dealloc)

    // don't forget the first line, otherwise you won't ger orientation. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

  2. Add constraint Bottom spacing View - Superview

  3. Connect this constraint to your code (as you connect outlets)
  4. If orientationChanged method set required value:

    self.constaint.constant = UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ? 10 : 200;

Right way

All code dealing with constraints should go to - (void) updateConstraints of UIView.

Upvotes: 1

matt
matt

Reputation: 535139

You're asking the button to move from all the way at the bottom to all the way at the top. That's not going to happen automagically! You'll to have move the button yourself in code. Implement viewWillLayoutSubviews on your view controller and position the button there.

Upvotes: 0

Related Questions