在iOS 6中获取当前界面方向

时间:2012-12-20 08:35:40

标签: objective-c ios xcode

我在iOS 5.x中动态进行方向检查,如下所示:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==         UIInterfaceOrientationPortraitUpsideDown) 
 {
self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
// some more settings
}
else
{
self.profileNew.frame = CGRectMake(374, 540, 295, 58);
 // some more settings
}
 return YES;
}

对于iOS 6,我做了以下代码,但那不起作用:

-(BOOL)shouldAutorotate{    
   return YES;
}

-(NSInteger)supportedInterfaceOrientations
{
 if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) 
  {
   self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
   // some more settings
  }
  else
  {
   self.profileNew.frame = CGRectMake(374, 540, 295, 58);
   // some more settings
  }
 return UIInterfaceOrientationMaskAll;
}

如何像在iOS 5.x中一样检查iOS 6中的界面内容?

由于

4 个答案:

答案 0 :(得分:9)

您可以使用viewWillLayoutSubviews方法检查方向

[[UIApplication sharedApplication] statusBarOrientation];

并相应地设置您的框架。

希望这有帮助!

答案 1 :(得分:4)

尝试检查此方法

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
                            duration:(NSTimeInterval)duration

编辑:

使用此:

BOOL isInPortrait = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]);

答案 2 :(得分:1)

结合Levi's和Nikola Kirev的答案,现在代码工作正常。谢谢你们两个。以下是其他人参考的代码:

对于iOS 6:

 -(BOOL)shouldAutorotate{
    return YES;
  }


-(NSInteger)supportedInterfaceOrientations{
        if (UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
            self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
            //other codes            
        }

        else {
            self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
            //other codes
        }
        return UIInterfaceOrientationMaskAll;
       }

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
                            duration:(NSTimeInterval)duration{
    if (UIDeviceOrientationIsPortrait(orientation)) {
        self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
        //other codes
        self.profileNew.frame = CGRectMake(238, 713, 295, 73);            
    }

    else {
        self.viewProfile.frame = CGRectMake(374, 462, 295, 58);
        //other codes
    }
}

适用于iOS 5.x和4.x

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        self.viewProfile.frame = CGRectMake(238, 612, 295, 73);
        //other codes      
    }

    else {
        self.viewProfile.frame = CGRectMake(374, 462, 295, 58); 
        //other codes
        }
    return YES;
    } 
}

答案 3 :(得分:0)

试试这个:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]