app 控制器的方向2025-07-17

appdelegate 里面的方向控制和页面方向设置的区别
image.png

注意:appdelegate 里面的方向会重写.plist 里面的方向

三、 app 通常采用UITabBarViewController 作为根视图控制器

目前的场景UITabBarViewController 接管了方向的控制权,所以通常这个类重写方向的方法

- (BOOL)shouldAutorotate
{
////增加自定义的业务需要
  return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
/// 需要找当前控制器的top 控制器,调用其supportedInterfaceOrientations ,这个就需要不同的业务控制器自定义重写了
///self.selectedViewController 
    return [self.selectedViewController supportedInterfaceOrientations];
   /// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

  • 通常需要重写自定义导航栏控制器,方便控制方向

- (UIStatusBarStyle)preferredStatusBarStyle
{
   return  [self.topViewController preferredStatusBarStyle];
}

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

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
   return self.topViewController.supportedInterfaceOrientations;
}
四、实际开发中的配合场景
1.全局统一方向:

AppDelegate 中返回 UIInterfaceOrientationMaskPortrait(仅允许竖屏)。
所有视图控制器无需额外设置(默认继承全局范围),应用全程只能竖屏。

2.全局支持多方向,局部限制:
  • AppDelegate 返回 UIInterfaceOrientationMaskAll(允许所有方向)。
  • 登录页控制器设置 supportedInterfaceOrientations 为 Portrait(仅竖屏)。
  • 视频页控制器设置 supportedInterfaceOrientations 为 Landscape(仅横屏),且 preferredInterfaceOrientationForPresentation 为 LandscapeLeft(默认左横屏)。
  • 效果:登录页强制竖屏,视频页强制横屏,切换时自动适配。
3.避免冲突:

若 AppDelegate 允许竖屏,而某个视图控制器的 supportedInterfaceOrientations 仅允许横屏,则两者无交集,会触发错误。此时需调整其中一方的范围,确保有重叠。
总结

  • AppDelegate 的方向控制是 “全局总开关”,决定应用能支持的最大方向范围;
  • 视图控制器的方向属性是 “局部调节器”,在全局范围内进一步限制方向或设置初始偏好。
    两者必须配合(确保有交集),才能实现灵活且无冲突的方向控制。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。