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

3

最初,NavigatorUINavigationController的子类,是一个根控制器,支持所有方向。该子类覆盖了supportedInterfaceOrientations方法,并提供属性来设置支持的方向。

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

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

这就是当某人无意中在联系人应用程序中以横向模式持有设备,但突然间其中一个子视图支持横向并自动旋转而不是先旋转设备到竖屏再旋转到横屏时应该如何实现的问题?

3个回答

1

在每个方法中使用此代码:

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

} 
else 
{

}

或者检查[[UIScreen mainScreen] bounds]

根据视图框架的尺寸,您可以确定其是否处于横向模式或纵向模式。 - Lokesh Chowdary
我是说你需要在每个函数中检查帧大小。 - Lokesh Chowdary

0

我认为这就是你想要的。

获取设备方向:

[[UIDevice currentDevice] orientation];

获取当前显示视图的方向:

[UIApplication sharedApplication].statusBarOrientation;

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];
    }
}

在 Stack Overflow 上找到了这个问题,但找不到链接。


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