You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter iOS端如何设置启动屏仅支持左右横屏方向?

Flutter iOS启动屏强制横屏解决方案

针对你遇到的启动屏在竖屏状态下显示异常的问题,给你两个可行的解决思路:

方案一:自定义启动屏视图控制器限制方向

  1. 在Xcode中新建一个LaunchScreenViewController类(Swift/OC均可),继承自UIViewController
  2. 在类中重写方向控制方法:
    • Swift代码:
      import UIKit
      
      class LaunchScreenViewController: UIViewController {
          override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
              return .landscapeLeft.union(.landscapeRight)
          }
      
          override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
              return .landscapeLeft
          }
      }
      
    • OC代码:
      #import "LaunchScreenViewController.h"
      
      @implementation LaunchScreenViewController
      
      - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
          return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
      }
      
      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
          return UIInterfaceOrientationLandscapeLeft;
      }
      
      @end
      
  3. 打开LaunchScreen.storyboard,选中根视图控制器,在Identity Inspector里把Class改成你刚创建的LaunchScreenViewController

方案二:通过AppDelegate动态控制方向

  1. 在AppDelegate中,先强制启动阶段仅支持横屏:
    • Swift代码:
      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
          // 启动时强制横屏
          UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
          return true
      }
      
      // 全局方向控制,根据当前页面动态返回
      func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
          // 判断当前是否需要允许竖屏,通过Flutter端的方法调用控制
          if let flutterVC = window?.rootViewController as? FlutterViewController {
              let channel = FlutterMethodChannel(name: "com.yourapp/orientation", binaryMessenger: flutterVC.binaryMessenger)
              var allowPortrait = false
              channel.setMethodCallHandler { call, result in
                  switch call.method {
                  case "allowPortrait":
                      allowPortrait = true
                      result(nil)
                  case "forbidPortrait":
                      allowPortrait = false
                      UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
                      result(nil)
                  default:
                      result(FlutterMethodNotImplemented)
                  }
              }
              return allowPortrait ? [.portrait, .landscapeLeft, .landscapeRight] : [.landscapeLeft, .landscapeRight]
          }
          return [.landscapeLeft, .landscapeRight]
      }
      
  2. 在Flutter端,打开相机前通过MethodChannel通知原生允许竖屏,关闭相机后通知禁止竖屏:
    final MethodChannel _orientationChannel = MethodChannel('com.yourapp/orientation');
    
    // 打开相机前调用
    await _orientationChannel.invokeMethod('allowPortrait');
    // 关闭相机后调用
    await _orientationChannel.invokeMethod('forbidPortrait');
    

两种方案都能解决启动屏竖屏显示的问题,方案一更直接针对启动屏,方案二适合需要全局动态切换方向的场景。

内容的提问来源于stack exchange,提问作者Savvy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 23:20:37