如何检测设备方向与界面方向不同?

时间:2013-03-14 13:06:44

标签: iphone ios objective-c uinavigationcontroller

最初,UINavigationControllerNavigator)的子类是根控制器,它支持所有方向。 子类重写supportedInterfaceOrientations并提供属性以设置支持的方向。

Navigator导航堆栈(UITableViewContreller的子类)的根视图控制器控制支持的方向(取决于哪个视图控制器位于堆栈顶部)。它会在Navigator覆盖中设置didSelectRowAtIndexPath方向属性。

如果在设备处于不同方向时进行转换(因为当前视图不支持它并且这不是所谓的交互方式)并且新视图支持该设备方向,则视图保持与设备不同的方向取向。 然后需要旋转设备并将其移回以带来正确的方向。

如果由于某种原因某人在联系人应用程序中以横向模式保存设备,但突然其中一个子视图将支持横向并自动旋转,而无需将设备旋转为纵向然后横向。问题是如何实现它?

3 个答案:

答案 0 :(得分:1)

在每种方法中使用它:

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
    ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{       

} 
else 
{

}

或查看[[UIScreen mainScreen] bounds]

答案 1 :(得分:0)

我认为这是你想要的。

要获取设备方向:

[[UIDevice currentDevice] orientation];

要获得所显示视图的当前方向:

[UIApplication sharedApplication].statusBarOrientation;

答案 2 :(得分:0)

将此方法添加到UINavigationController的子类:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        return;

    if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) {
        //present/dismiss viewcontroller in order to activate rotating.
        UIViewController *mVC = [[UIViewController alloc] init];
        [self presentModalViewController:mVC animated:NO];
        [self dismissModalViewControllerAnimated:NO];
    }
}

(在SF上找到它,但找不到该问题的链接)