iPhone / iPad应用方向(iPhone/iPad App Orientation)

2019-07-31 19:24发布

我的应用程序的iPhone版本支持UIDeviceOrientationPortraitUpsideDownUIDeviceOrientationPortrait ,但iPad版本支持所有方向。

在我的视图控制器我有这样的:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation     {
    return YES;
}

和我的Info.plist文件中有这样的:

问题是,当我构建应用程序到我的iPod,应用程序将不会上下颠倒。 而iPad将支持所有取向。

我试图消除shouldAutorotateToInterfaceOrientation一起,我已经试过这样的代码:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation = UIDeviceOrientationLandscapeLeft) && (interfaceOrientation = UIDeviceOrientationLandscapeRight) && (interfaceOrientation = UIDeviceOrientationPortraitUpsideDown) && (interfaceOrientation = UIDeviceOrientationPortrait);
}

但由于某些原因,倒挂是行不通的! 有没有这方面的任何其他解决办法?

编辑:使用的Xcode 4.5 iOS6的

Answer 1:

想通了,在iOS6的SDK使用shouldAutorotate所以这里是我的新代码:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

-(BOOL)shouldAutorotate {
     return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}


Answer 2:

如果我理解正确的话,你想在iPad支持所有方向,而你想在iPhone上支持倒挂和纵向的旋转刚。 试试这个作为一个解决方案。 (这是一个简单的方法,那么你在上面做什么)

   -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
          return YES;
     }

    else {
            return UIInterfaceOrientationIsPortrait(interfaceOrientation);
        }
 }

我还想补充一点,你必须按顺序加入到这个每个单独视图控制器为它的其他视图旋转。

举例来说,可以说我有viewcontroller_1和viewcontroller_2,我必须去到控制器的两个.m文件,并添加以下代码。 如果你不,可能不会进行自动旋转的观点之一。



Answer 3:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

这种方法在弃用 IOS 6 ,从而代替它,你可以使用下面的方法。

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

根据返回的方向。



Answer 4:

支持方向的另一种方式是继承的UINavigationController,并添加您的方向代码中有

//IOS 5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES;
}
//IOS 6
- (BOOL)shouldAutorotate {
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

这个工作对我来说,用故事板



文章来源: iPhone/iPad App Orientation