简体   繁体   English

仅为一个视图控制器设置方向 iOS 13 Swift

[英]Set Orientation For Only One View Controller iOS 13 Swift

I'm creating an iPhone-only app using the latest version of Swift and Xcode, with 3 tabBar sections in the app, and in only one tab bar I want the user to have its screen fixed to landscape.我正在使用最新版本的 Swift 和 Xcode 创建一个仅限 iPhone 的应用程序,该应用程序中有 3 个 tabBar 部分,并且我希望用户仅在一个选项卡栏中将其屏幕固定为横向。 I've tried almost all possible solutions I found on StackOverflow and some other websites, all not seeming to work (including this one: How to lock orientation of one view controller to portrait mode only in Swift ).我已经尝试了我在 StackOverflow 和其他一些网站上找到的几乎所有可能的解决方案,但似乎都没有奏效(包括这个: 如何仅在 Swift 中将一个视图控制器的方向锁定为纵向模式)。 Anyone know why?有谁知道为什么?

This is the view controller of the view that I want to be fixed in landscape orientation:这是我想在横向固定的视图的视图控制器:


import UIKit
@objcMembers class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }

        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        themes()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    //Some Code

///////////////////////////////////////

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

    override var shouldAutorotate: Bool {
        return false
    }
///////////////////////////////////////
}

You can force application to rotate orientation using below code,您可以使用以下代码强制应用程序旋转方向,

UIDevice.current.setValue(.landscapeRight, forKey: "orientation")

Please implement below delegate method in AppDelegate and return landscape mask when you want application to rotate to landscape mode,请在 AppDelegate 中实现以下委托方法并在您希望应用程序旋转到横向模式时返回横向蒙版,

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果应用程序设备的方向是纵向的,那么如何仅在iOS中为一个视图构建者设置强制横向,Swift 2.3 - if app device orientation is portrait how to set force landscape only for one view constroller in ios, swift 2.3 iOS7。仅更改一个视图控制器的页面方向 - iOS 7. Change page orientation only for one view controller iOS 7 - 仅在一个视图控制器中限制横向方向 - iOS 7 - Restrict landscape orientation only in one view controller iOS:如何更改应用程序中仅一个视图控制器的方向? - iOS: How to change Orientation of only one view controller in app? 一种View Controller iOS的横向 - Landscape orientation for one view controller ios 只有一个视图控制器响应方向设置 - Only One View Controller Responds To Orientation Settings 设备方向纵向仅适用于一个视图控制器 - Device orientation portrait only for one view controller iOS 13:Swift - '以编程方式设置应用程序根视图 controller' 不起作用 - iOS 13: Swift - 'Set application root view controller programmatically' does not work Swift 4设置仅用于一个View Controller的导航属性 - Swift 4 Set Navigation Attributes for only one View Controller 仅支持iOS 6的一个视图的不同方向 - Support different orientation for only one view iOS 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM