Search code examples
objective-ciosxcodexiblandscape

Landscape xib for ipad version only


I have a universal app with seperate xibs for iPhone and iPad

For the iPad version, I want my one of my iPad xibs in landscape mode. I have set it to landscape in simulated metrics, (The app is set for portrait in the summary screen) and the landscape view still shows in portrait when run. Im sure I read on here a naming convention that tells the app the xib is in landscape ie: view1_one.xib~ipad-landscape

Ive tried using screen rotation methods but iOS 6 screen rotation is just proving so frustrating as I have tried so many solutions all of which have no effect. I have done screen rotation before in iOS 6 but I was using storyboards and subclassed a navigation controller for my chosen view, I dont know how to do this with this code as I picked it ip from someone else

So to sum up: How can I get my screen to respect the landscape xib I have created in interface builder instead of showing it in portrait

I would point out I have searched on here for a solution and spent many hours on this before posting this


Solution

  • There are naming conventions for resources (including xib's) that specify on which device type they are loaded. However, these do not exist for specific orientations. -landscape won't do anything extra (more specifically, it will probably load the resource on an iPhone as well, which isn't what you want).

    http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html#//apple_ref/doc/uid/10000051i-CH1-SW2

    As for handling orientation changes, make sure you implement both the iOS 5 and below methods and the iOS 6 methods for handling them:

    // Older versions of iOS (deprecated)
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
    }
    
    // iOS6
    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    // iOS6
    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    // Added after comment
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationLandscapeLeft;
    }
    

    This is the official way to ensure that your viewController is only loaded in landscape.

    Edit:

    I added the preferredInterfaceOrientationForPresentation method as well. While personally I don't like this method, it forces the viewController to present in that orientation, until the next orientation change. Not nice, but it will help you to see if your orientation methods get called at all.

    Also try to set a breakpoint in each of these methods. If they don't get called at all, your issue might be with the subclassing. Be certain that the viewController in the storyboard is set to the subclass and not the superclass.

    Edit2:

    Check this in Interface Builder:

    Make sure that the ViewController in Interface Builder is set to the appropriate subclass