iPhone locked Portrait, iPad locked Landscape

2019-08-05 07:35发布

问题:

I'm trying to convert an iPhone app to iPad. The tricky things is that the iPhone app has to be locked to portrait view and the iPad app has to be locked to landscape view. I'm kind-of a noob at interface builder so I'm a little lost.

Any help is greatly appreciated. Thanks!

回答1:

You need to override shouldAutorotateToInterfaceOrientation. A good place to put this is in the app delegate.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && 
        UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
        return YES;
    }
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && 
        UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
    {
        return YES;
    }   
    return NO;
}

I'm not sure, but this may also support the phone being upside-down, which is a HIG no no. You might want to use interfaceOrientation == UIInterfaceOrientationPortrait instead for the phone.



回答2:

You should have two nibs and load them separately depending on which device your application determines it is running on: one for the iPad and one for the iPhone. You can then set the orientation easily. NB: The iPad app should support all orientations variants (ie. if you support portrait, support portrait upside-down) and will most likely get rejected by Apple unless you have a compelling reason as to why it shouldn't.