(Masonry 底层就是 AutoLayout 的 NSLayoutConstraint)
git
// 三维设备方向 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 }; // 二维界面方向 typedef NS_ENUM(NSInteger, UIInterfaceOrientation) { UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown, UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft }; // iOS6 之后引入组合方式 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), };获取设备方向:[[UIDevice currentDevice] orientation]
// 返回是否支持屏幕旋转 - (BOOL)shouldAutorotate { return YES; } // 返回支持的旋转方向 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; } // 返回优先显示的屏幕方向,假设不设置,默认与进入前一个页面保持一致(注意该方法仅仅对 ModalViewController 有效) - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft; }
普通状况下 ChildrenViewController 不单独设置。与 RootViewController 保持一致。假设特殊场景需要单独设置,可以经过在 RootViewController 中下放权限。如:NavigationController 可以经过 self.topViewController 下放权限;TabBarController 可以经过 self.selectedViewController 下放权限。但是要注意,即便下放了权限,ChildrenViewController 仍是必须遵照 Info.plist 和 AppDelegate 中的设置。
(5)假设存在弹出的 ModalViewController,则不受限于步骤4中的 RootViewController,仅仅依据 Info.plist、AppDelegate 及其自身所支持的旋转设置决定是否旋转。假设 ModalViewController 是通过包装的还有一个 RootViewController。则与步骤4原理相似。
github
在 Info.plist 中锁死指定方向。限制屏幕旋转。
(2)应用统一支持多个方向本身主动旋转。在 Info.plist 中设置应用支持的方向,每个页面进行对应的本身主动旋转布局适配。ide
(3)应用不一样页面支持的方向不一致。在 Info.plist 中设置所有页面支持的方向的并集。在 RootViewController 中将权限下放,由页面但与控制本身的旋转设置。布局
适配方式纯代码的话相同建议 Masonry。
演示样例:SingleRotationDemo
(4)应用场景4:应用支持单一方向,但是个别页面支持手动控制旋转。不支持本身主动旋转。(通常不建议使用,除非特定场景。如视频播放器页面限制本身主动旋转,点击全屏button后横屏观看)
思路:有两种强制方式旋转方式,一种是带导航栏的,一种是不带导航栏的。具体实现思路演示样例中有具体描写叙述。
演示样例:ForceRotationDemo
post
苹果官方也是支持这样的方式。spa