我有一个UIWebView
我想加载不同的 URL,具体取决于它的纵向还是横向。我怎样才能弄清楚我当前的方向是viewWillAppear
什么?
问问题
5993 次
4 回答
14
使用 UIApplication 的statusBarOrientation
属性。如果您使用设备方向(如果是UIDeviceOrientationFaceUp
或),您可能会遇到问题UIDeviceOrientationFaceDown
。
例子
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
// Do something when in landscape
}
else
{
// Do something when in portrait
}
于 2011-10-31T18:11:26.230 回答
4
UIDeviceOrientation getCurrentOrientation() {
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation currentOrientation = device.orientation;
[device endGeneratingDeviceOrientationNotifications];
return currentOrientation;
}
将 转换为 由您UIDeviceOrientation
决定UIInterfaceOrientation
。
于 2011-10-31T18:05:28.667 回答
2
当应用程序加载时,它不知道它的当前方向 -
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
NSLog(@"portrait");// only works after a rotation, not on loading app
}
旋转设备后,您将获得正确的方向,但是在加载应用程序时,在不更改方向的情况下,似乎 using[[UIDevice currentDevice] orientation]
不知道当前方向。
所以你需要做两件事 -
- 尝试在 plist 文件中设置应用程序接受的设备方向
- 在您的 UIViewControllers 中,您将需要覆盖该
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
方法以在应用程序应该返回 YES 时rotate:
于 2011-10-31T17:57:49.757 回答
1
如果相关调用的 is 顺序意味着您不能依赖interfaceOrientation
当时的正确值viewWillAppear
(假设父视图控制器旋转),那么最安全的可能是self.parentViewController.interfaceOrientation
. 如果您无法尝试从 做出第一个假设[[UIDevice currentDevice] orientation]
,这可能并不总是 100% 的钱(例如,如果您的应用程序启动时设备平放在桌子上),但可能比总是假设给您更好的猜测肖像。
于 2011-10-31T18:01:27.320 回答