0

我有一个支持横向和纵向模式的拆分视图控制器。当详细视图控制器在某个视图上时,我禁用纵向并且只允许横向。但是,如果用户离开该特定视图,将设备旋转到纵向,然后返回到该特定视图,它仍然是纵向的。我需要它自动旋转回横向。下面是我正在使用的。这些方法都是从我的子类 UISplitViewController 调用的,因此方向可以依赖于视图控制器。

#pragma mark - Orientation

- (BOOL)shouldAutorotate
{
    // Allow view controller to rotate between landscapes
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    // Return supported interface orientation
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    // Return preferred orientation
    return UIInterfaceOrientationLandscapeLeft;
}
4

1 回答 1

0
// This has worked for me - assumes ARC is enabled

- (void)forceLandscape
{
    UIDevice  *myDevice = [UIDevice currentDevice];
    if([myDevice respondsToSelector:@selector(setOrientation:)])
    {
        NSInteger param     = UIInterfaceOrientationLandscapeRight;
        NSMethodSignature *signature  = [[myDevice class] instanceMethodSignatureForSelector:@selector(setOrientation:)];
        NSInvocation      *invocation = [NSInvocation invocationWithMethodSignature:signature];
        [invocation setTarget:myDevice];
        [invocation setSelector:@selector(setOrientation:)];
        [invocation setArgument:&param
                        atIndex:2];
        [invocation invoke];
    }
}
于 2014-09-10T18:37:52.730 回答