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

7

我在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 6中像在iOS 5.x中一样检查接口方向?

谢谢

4个回答

9
您可以使用viewWillLayoutSubviews方法,其中您可以通过 [[UIApplication sharedApplication] statusBarOrientation]; 检查方向并相应地设置框架。希望这可以帮到您!

请问您能否写下使用[UIApplication sharedApplication] statusBarOrientation]进行检查的代码吗? 例如,if ([UIApplication sharedApplication] statusBarOrientation] == "我需要在这里使用什么常量") - Noufal Kmc
1
它和你的代码完全一样:if (UIDeviceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) { ... } else { ... }。使用 viewWillLayoutSubviews 的另一个好处是,如果你在其中更改框架,它将会被动画化。 - Levi
结合了你和Nikola Kirev的回答,现在代码可以正常工作了。感谢你们俩。 - Noufal Kmc
1
很高兴能帮忙,但我在stackoverflow上还是新手,我该如何给你们点赞?我已经点击了“是”按钮,表示这篇文章对我有用,它显示在评论框下方。 - Noufal Kmc

4

尝试在此方法中执行检查

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

编辑:
使用以下内容:
BOOL isInPortrait = UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]);

这将在应用程序启动时调用,然后在我们旋转设备时调用。我想从一开始就检查当前方向,即在启动应用程序时而不旋转设备。 - Noufal Kmc
检查[[UIDevice currentDevice] orientation]未获得正确结果。这就足够了:
  • (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration{ if (UIDeviceOrientationIsPortrait(orientation)) {
    }
    else {
    } }
结合Levi和你的答案,现在代码正常工作。感谢你们两个。
- Noufal Kmc
设备方向可能是未知的。您需要界面方向。 - Danra

1

结合了Levi和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;
    } 
}

[UIDevice orientation] 返回的是 UIDeviceOrientation,而不是 UIInterfaceOrientation。 - Danra
是的,我想在更改设备方向时设置界面方向。 - Noufal Kmc
它们是两个不同的枚举类型。将一个赋给另一个将会给你带来意想不到的结果。 - Danra
但我这里没有赋值,只是在检查。抱歉,我不是在和你争论。我只有1.8年的iOS开发经验,上面的代码我用于一个应用程序,而且运行得非常完美。这是应用程序链接: https://itunes.apple.com/app/id544584878 - Noufal Kmc

0

试一下这个:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]

但是在iOS 6中,常量UIInterfaceOrientationPortrait无法正常工作。 - Noufal Kmc
-[UIDevice orientation] 返回的是 UIDeviceOrientation,而不是 UIInterfaceOrientation - Danra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接