chrisjr
chrisjr

Reputation: 770

Disable orientation in certain View Controllers if device is iPhone

I'm trying to make my app disable landscape orientation if the following conditions are met:

I've tried this in AppDelegate.m:

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {

        if ([[window.rootViewController presentedViewController] isKindOfClass:[WelcomeViewController class]])
            return UIInterfaceOrientationMaskPortrait;
        else
            return UIInterfaceOrientationMaskAllButUpsideDown;

    }
}

This doesn't work since I get an error, saying Control may reach end of-non-void function.

I also tried this:

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;

    if ([deviceModel rangeOfString:@"iPhone"].location != NSNotFound && [[window.rootViewController presentedViewController] isKindOfClass:[WelcomeViewController class]]) {

        NSLog(@"I am an iPhone");
        return UIInterfaceOrientationMaskPortrait;

    } else if ([deviceModel rangeOfString:@"iPhone"].location != NSNotFound && [[window.rootViewController presentedViewController] isKindOfClass:[LogInViewController class]]) {

        NSLog(@"I am an iPhone");
        return UIInterfaceOrientationMaskPortrait;

    } else if ([deviceModel rangeOfString:@"iPhone"].location != NSNotFound && [[window.rootViewController presentedViewController] isKindOfClass:[SignUpViewController class]]) {

        NSLog(@"I am an iPhone");
        return UIInterfaceOrientationMaskPortrait;

    } else {

        NSLog(@"I am NOT an iPhone");
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

If I test it on an iPhone, however, it will go straight into the else statement, which is interesting, since if I remove [[window.rootViewController presentedViewController] isKindOfClass:[SignUpViewController class]], it will work.

What am I doing wrong?

Upvotes: 0

Views: 80

Answers (1)

matt
matt

Reputation: 536027

This doesn't work since I get an error, saying Control may reach end of-non-void function.

Well, think about your logic here (or lack of it):

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
        if ([[window.rootViewController presentedViewController] isKindOfClass:[WelcomeViewController class]])
            return UIInterfaceOrientationMaskPortrait;
        else
            return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

If UI_USER_INTERFACE_IDIOM() is UIUserInterfaceIdiomPad, you return something in every situation (both if and else). But otherwise, you don't say what to do! What if UI_USER_INTERFACE_IDIOM() is not UIUserInterfaceIdiomPad? What do you return then? Nothing! So this makes no sense. You are supplying insufficient information. You must cover every possible case, returning something no matter what may happen.

Personally, I think the entire way you are going about this is very silly. What I would do is implement supportedInterfaceOrientations in each of the three classes WelcomeViewController, LogInViewController, and SignUpViewController. In each implementation I would then express exactly what you said at the outset: return any orientation if the device is an iPad, but portrait only if the device is an iPhone. Yes, this involves some repetition — the implementation of supportedInterfaceOrientations will be identical in all three view controllers - but who cares? At least it will be understandable - and it will work, which is more than you've got going for you right now.

Upvotes: 1

Related Questions