3

In FirstViewController there is a button. After it is pressed, there is a modal segue that goes to SecondViewController. FirstViewController is Portrait, and SecondViewController is Landscape. In the storyboard file, I set SecondVC to Landscape but the iOS Simulator won't automatically change it's orientation.
Can someone help me find code that automatically turns SecondViewController from Portait to Landscape?

viewDidLoad statement in SecondVC:

-(void)viewDidAppear:(BOOL)animated  {
    UIDeviceOrientationIsLandscape(YES);
    sleep(2);
    santaImageTimer = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                       target:self
                                                     selector:@selector(santaChangeImage)
                                                     userInfo:NULL
                                                      repeats:YES];
    [santaImageTimer fire];
    image1 = YES;
}

Any help appreciated.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
DHShah01
  • 553
  • 1
  • 7
  • 16

1 Answers1

4

Sadly, while your attempt to call UIDeviceOrientationIsLandscape(YES); was a valiant attempt, that doesn't actually change orientation. That method is used to confirm whether a variable holding a orientation is landscape or not.

For example, UIInterfaceOrientationIsLandscape(toInterfaceOrientation) will return TRUE if the toInterfaceOrientation holds a landscape orientation, and FALSE if not.

The correct technique for changing orientation is outlined in Handling View Rotations in the UIViewController Class Reference. Specifically, in iOS 6, you should:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

In iOS 5, the necessary method is:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
        return YES;
    else
        return NO;
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • By the way, pre iOS6, sometimes forcing orientation change is problematic. See http://stackoverflow.com/questions/11366929/force-rotate-view-controller/11368949#11368949 for a workaround. – Rob Dec 24 '12 at 06:47
  • 1
    Thanks! The iOS 6 one worked. Will try iOS 5. Do you know how to detect if the user's device is running iOS 6? – DHShah01 Dec 24 '12 at 18:08