一、重载根控制器

@property(nonatomic, readonly) UIInterfaceOrientationMask supportedInterfaceOrientations
在这个属性的文档中有如下解释:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window.

iOS 6之后,控制器的方向由根控制器管理,可使用继承或者分类的方式,重载根控制器关于旋转屏幕的三个方法,将方向的选择权归还给子控制器。

@implementation UINavigationController (manualRotate)

- (BOOL)shouldAutorotate {
    return self.topViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return self.topViewController.preferredInterfaceOrientationForPresentation;
}

@end


@implementation UITabBarController (manualRotate)
//在`iOS 5`及更早的版本中默认返回`NO`, 其他默认返回`YES`
- (BOOL)shouldAutorotate {
    return self.selectedViewController.shouldAutorotate;
}

// shouldAutorotate为Yes时才会被调用
// iPad默认返回UIInterfaceOrientationMaskAll
// iPhone默认返回UIInterfaceOrientationMaskAllButUpsideDown
// 此方法的返回值与AppDelegate的application:supportedInterfaceOrientationsForWindow:的交集才是有效值
// AppDelegate的application:supportedInterfaceOrientationsForWindow:会覆盖工程配置中(Info.plist)的Supported interface orientations
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return self.selectedViewController.supportedInterfaceOrientations;
}

// 系统会默认按statusBar的方向来展示一个新的控制器,即默认按当前方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}

@end

至此,新的控制器,只需要重载上面的方法就能管理自己的方向了。

二、强制修改设备方向

@implementation UIDevice (manualRotate)
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[self currentDevice]];
    NSInteger val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
}
@end

三、使用

  1. 父控制器为竖屏,子控制器为横屏,只需要在自控制器重写三个方法即可。注意需要控制好父、子控制器之间方向的关系。
  2. 同一个控制器,需要在竖屏和横屏之间切换,如带有视频播放的控制器。需要重写上面的方法,然后调用[UIDevice setOrientation: orientation] 方法进行屏幕旋转。
  3. swift iOS 16 点击切换横竖屏_竖屏

四、布局

同一控制器在横竖屏之间切换,经常需要不同的布局。在iOS 8及以后的系统可以使用SizeClass方便地部署横竖屏的AutoLayout。在Xcode 8及更高版本后,界面有所改变。

swift iOS 16 点击切换横竖屏_竖屏_02

所有不是在Vary for Traits 下添加的约束会被当做为所有尺寸的设备添加的约束。
点击Vary for Traits 选择宽高,可以为指定的设备(群)添加约束。

五、方向说明

UIDeviceOrientation 用于表示设备的物理方向

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
} __TVOS_PROHIBITED;

UIInterfaceOrientation 用于表示界面的方向,当设备向右旋转时,UI需要向左旋转,所以在LeftRight两个方向上,UIInterfaceOrientationUIDeviceOrientation是相反的。

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
}

UIInterfaceOrientationMask ,在单个方向上与UIInterfaceOrientation并无区别,只是多了UIInterfaceOrientationMaskLandscapeUIInterfaceOrientationMaskAllUIInterfaceOrientationMaskAllButUpsideDown等表示界面方向的集合。

typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
}