0

Trying to get my iPad app to work in landscape orientation. I would like to change the positions of some of my controls once in landscape mode. I get the log so I know I'm in the method, however, none of my objects move. Am I missing something?

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation==UIInterfaceOrientationLandscapeRight ||
   toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft)
    {
        NSLog(@"in landscape");
        // I would like to re-position my controls here but not working
        image1.frame = CGRectMake(318, 501, 100, 100);
        btnNext.frame = CGRectMake(200, 455, 100, 35)
    }
}
mablecable
  • 385
  • 1
  • 6
  • 15
  • I tested this against my iPad simulator 5.0 and it works here. Looks like it is just an issue with the newer iOS. How do I handle this?? Using storyboards so I would like to support 5.0 and up. Advice? – mablecable Apr 07 '13 at 04:48

2 Answers2

0

Your code works with iOS 5, but for iOS 6 and above try using orientation mask. Here is a method where you can implement the rotation of your objects inside it.

- (BOOL) shouldAutorotate
{
return YES;
 }

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft   | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

You can remove the masks that you don't need. In iOS 6 after by inputing the objects that you want within this method, you should archive what you are trying to do.

Adrian P
  • 6,479
  • 4
  • 38
  • 55
  • Ok great, this helps! But how can I tell which orientation my app is launched in? If opened in landscape, the view displays like portrait. – mablecable Apr 07 '13 at 21:12
  • If you want to set the orientation of the app in one direction there are two was to do it. One is at the target level of your project on the information page just un highlight the view orientations you don't want. Two is through code which you have it already. Also in storyboard you can set the view to landscape or portrait. If you want landscape orientation then do the highlight and use the masks code and it'll do it. Hope this helps clearing it up. Let me know if I can help you in any way. – Adrian P Apr 08 '13 at 05:00
0

Try moving your repositioning code into viewWillLayoutSubviews.

If you really want to go all the way, use constraints in iOS 6 only. Your iOS 6 code will be very, very different from your iOS 5 code, which is annoying, but it will also be much more elegant. In this discussion from my book, I go into some depth on the problem of rearranging the interface in response to rotation:

http://www.apeth.com/iOSBook/ch19.html#SECrotationevents

An elegant solution I evolve in that discussion is to set up the interface using constraints in the first place, and then implement updateViewConstraints as a place to adjust those constraints in response to rotation. The discussion then goes on to show how to use viewWillLayoutSubviews to set up the initial interface in the case where the app launches into landscape; but again, as it then points out, if you can use constraints instead, so much the better.

matt
  • 515,959
  • 87
  • 875
  • 1,141