Carpetfizz
Carpetfizz

Reputation: 9149

iOS 7 Stop Autorotate

I'm trying to lock orientation of my entire app to portrait modes, so I changed the info.plist, to this:

enter image description here

However, if I rotate the simulator or even my real device, the view still rotates. Any explanation for this? Thanks!

Upvotes: 0

Views: 147

Answers (1)

Refael.S
Refael.S

Reputation: 1644

You need to implement this in your ViewController:

For iOS 6 and up:

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

#endif
#endif

For iOS under 6:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    BOOL returnBool = YES;

    if (interfaceOrientation == UIInterfaceOrientationPortrait) {
        returnBool = YES;
    }
    else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        returnBool = YES;
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        returnBool = NO;
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        returnBool = NO;
    }
    else {
        returnBool = NO;
    }

    return returnBool;
}

The pre-processor statements are to make sure the method __IPHONE_OS_VERSION_MAX_ALLOWED exists

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

and the second one:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000

is to make sure the iOS version running on the device is 6 and up because the methods in the pre-processor statements do not exist on versions under iOS 6. It will crash on iOS under 6 without the pre-processor statements.

Upvotes: 3

Related Questions