【问题标题】:iOS Force splitViewController to rotate to LandscapeiOS 强制 splitViewController 旋转到横向
【发布时间】:2023-07-24 16:37:01
【问题描述】:

我有一个支持横向和纵向模式的拆分视图控制器。当详细视图控制器在某个视图上时,我禁用纵向并且只允许横向。但是,如果用户离开该特定视图,将设备旋转到纵向,然后返回到该特定视图,它仍然是纵向的。我需要它自动旋转回横向。下面是我正在使用的。这些方法都是从我的子类 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;
}

【问题讨论】:

    标签: ios uiviewcontroller uisplitviewcontroller uiinterfaceorientation autorotate


    【解决方案1】:
    // 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];
        }
    }
    

    【讨论】: