无法在ipad应用中设置方向

时间:2013-01-16 11:18:46

标签: iphone ios objective-c ipad ios5

我正在使用ios6版本来生成新的Ipad应用程序。在我的应用程序中,我正在创建一个拆分视图。分割视图必须始终处于横向模式。应用程序正在ipad 6.0模拟器中工作。但不适用于ipad 5.0模拟器。我想运行ipad 6.0和ipad 5.0的应用程序。

我正在使用此代码

-(NSUInteger)supportedInterfaceOrientations
{
     return UIInterfaceOrientationMaskLandscape;
}

-(BOOL) shouldAutorotate {
     return NO;
}

2 个答案:

答案 0 :(得分:0)

在iOS 5.0中,您应该使用:

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

答案 1 :(得分:0)

你可以用ios6这样做: -

-(BOOL)shouldAutorotate
{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{

    return UIInterfaceOrientationMaskAll;
}

并每次在ViewWillApear设备Oriantation中检查,如: -

- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
        if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
        {
            if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {

              //set your landscap View Frame
                [self supportedInterfaceOrientations];

            }



        }
        else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        {
            if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
      //set your Potrait View Frame
                [self supportedInterfaceOrientations];

            }
        }
        // Handle rotation
    }


    -(void)viewWillAppear:(BOOL)animated
    {
        [self willRotateToOrientation:[[UIDevice currentDevice] orientation]];  
        [super viewWillAppear:YES];
    }

<强>更新

可能有人使用检查设备orientation,如下所示将此行放入ViewWillApear: -

[[UIApplication sharedApplication] statusBarOrientation];
    [[UIDevice currentDevice] orientation];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];

-(void)deviceRotated:(NSNotification*)notification
{

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        //Do your stuff for landscap
    }
    else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
      //Do your stuff for potrait

    }

}

在IOS5中,你只能做下面的景观:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {


        return YES;
    }
    else
    {
        return NO;
    }
}

if you wish to support all oriantation you need to just return YES  like:-

 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

            return YES;  
    }