如何在M1 Mac上禁用iOS UIKit应用的横屏显示?
解决方法
- 调整Info.plist配置
首先确认项目Info.plist中的UISupportedInterfaceOrientations数组仅包含UIInterfaceOrientationPortrait,移除其余所有方向的配置项。
新增布尔类型的键UIApplicationSupportsIndirectInputEvents,值设为NO,该配置会禁用M1 Mac运行iOS App时的窗口缩放、横屏切换等桌面端交互特性。 - 代码强制锁定竖屏方向
若使用Scene生命周期,在SceneDelegate中添加如下代码:
若使用传统AppDelegate生命周期,添加如下方法:func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) { guard UIDevice.current.userInterfaceIdiom == .phone else { return } windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait)) }func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return .portrait } - 固定运行窗口大小
在Xcode项目的「General」-「Deployment Info」配置区段,找到「Designed for iPhone」的Mac适配配置,勾选 Prefer window size matching iPhone device 选项,强制应用在M1 Mac上运行时固定为iPhone竖屏尺寸,禁止用户手动调整窗口。
内容的提问来源于stack exchange,提问作者XYZ
相关产品推荐
相关产品推荐

