没有完美的方法来检测iPad上的设备方向? [英] No perfect way to detect device orientation on iPad?

查看:100
本文介绍了没有完美的方法来检测iPad上的设备方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的iPad启动时,我会在设备上运行。显示一个加载标签,居中在屏幕中间。

When my iPad starts up, I show a loading label, centered in the middle of the screen. I set its autoresizingMask so it recenters on orientation change.

随着应用程序启动,标签的文本改变,所以我想根据新的长度重新标记标签。但是,以下代码片段没有正确定位标签的中心:

As the app starts up, the label's text changes, so I want to recenter the label based on its new length. However, the following piece of code doesn't center the label correctly:

- (void) setLabelText:(NSString*)text {
  CGSize maximumLabelSize = CGSizeMake(500,20);
  CGSize expectedLabelSize = [text sizeWithFont:loadingLabel.font
                              constrainedToSize:maximumLabelSize
                                  lineBreakMode:loadingLabel.lineBreakMode];
  loadingLabel.frame = CGRectMake(self.view.frame.size.width/2-expectedLabelSize.width/2,
                                  loadingLabel.frame.origin.y, 
                                  expectedLabelSize.width, 
                                  loadingLabel.frame.size.height);
  loadingLabel.text = text;
}



我还考虑检查[[UIDevice currentDevice] orientation],如果iPad是横向模式,然后我将使用self.view.frame.size.height设置标签的xOrigin。

I also considered checking [[UIDevice currentDevice]orientation], and if the iPad is in landscape mode, then I'd use self.view.frame.size.height to set the xOrigin of the label.

但是,如果设备是正面向上或面朝下(而不是横向或纵向),则此方法失败。我还在我的appDelegate中有一个lastOrientation变量,它记住应用程序是横向还是纵向,即使面朝上或面朝下,基于设备的最后一个已知的方向。但是,在启动时,此变量不一定设置。

However, if the device is face up or face down, (and not landscape or portrait) then this method fails. I also have a lastOrientation variable in my appDelegate, which remembers if the app is in landscape or portrait, even when face up or face down, based on the device's last known orientation. However, at start-up, this variable isn't necessarily set.

有没有一些简单的解决方案,我在这里缺少,所以我可以调整大小和中心我的标签?

Is there some simple solution I am missing here, so I can resize and center my label?

编辑:我尝试根据发布的建议检查UIStatusBarOrientation,但它不工作:

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
    || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
  NSLog(@"landscape");  
  width = self.view.frame.size.height;
} else {
  NSLog(@"portrait");   
}

这总是在模拟器上记录肖像,至少在启动时。

This always logs portrait, at least on start-up, on the simulator.

推荐答案

检查 [[UIApplication sharedApplication] statusBarOrientation]

这篇关于没有完美的方法来检测iPad上的设备方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆