ioscocoa-touchuiinterfaceorientation

Code to turn orientation to landscape programmatically?


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.


Solution

  • 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;
    }