【问题标题】:Lock orientation in single Swift VC with Objective C AppDelegate使用 Objective C AppDelegate 在单个 Swift VC 中锁定方向
【发布时间】:2017-10-26 13:26:41
【问题描述】:

我有一个 Objetive-C 应用程序委托,它是我与我自己的交互项目的一部分。我正在尝试锁定方向,但看起来旧方法(如下)不再有效。在viewDidLoad 中设置方向会旋转屏幕,但允许用户旋转回portrait。我尝试转换为"override var",但没有运气。当前的解决方案(下面的链接)看起来都涉及app delegate 调用,但我找不到对Objective C 应用程序委托的Swift 调用的解决方案。

How to lock orientation of one view controller to portrait mode only in Swift

 override func shouldAutorotate() -> Bool {
    return false
 }

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscapeRight
 }

【问题讨论】:

标签: ios objective-c iphone swift


【解决方案1】:

强制只有一个控制器处于横向。 经理:

class OrientationManager {

    static let shared = OrientationManager()

    /// you can set any orientation to lock
    var orientationLock = UIInterfaceOrientationMask.portrait

    /// lock orientation and force to rotate device
    func lock(for orientation: UIInterfaceOrientationMask, rotateTo rotateOrientation: UIInterfaceOrientation) {
        orientationLock = orientation
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }
}

用法:

1) 将代码添加到 AppDelegate

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return OrientationManager.shared.orientationLock
}

2) 在控制器中使用

open class LandscapeController: UIViewController {

    /// orientation for one controller
    override open func viewDidLoad() {
        super.viewDidLoad()
        OrientationManager.shared.lock(for: .landscape, rotateTo: .landscapeLeft)
    }

    /// set previous state of orientation or any new one
    override open func viewWillDisappear(_ animated : Bool) {
        super.viewWillDisappear(animated)
        OrientationManager.shared.lock(for: .portrait, rotateTo: .portrait)
    }
}

【讨论】:

  • @rtnlsltn 你是什么意思“Obj-C”? swift和obj-c没有区别
【解决方案2】:

在尝试了几乎所有方法之后,使用此链接解决了我的问题。 how to lock portrait orientation for only main view using swift

我利用 JasonJasonJason 的答案并更新了代码以摆脱 ->.

下面的代码进入了我的第一个 VC

extension UINavigationController {
override open var shouldAutorotate: Bool {
    return true
}

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return (visibleViewController?.supportedInterfaceOrientations)!
}
}

然后是后续的 VC

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscape
}

然后,由于我有一个 TabBarController,我将它添加到推送到选项卡视图的控制器中。如果您尝试重新使用初始 UINavigationController 扩展,它不会让您这样做,但 TabBarController 会忽略它。所以下面的扩展为任何带有 TabBar 的 VC 解决了这个问题

extension UITabBarController {
override open var shouldAutorotate: Bool {
    return true
}

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscape
}
}

【讨论】:

    【解决方案3】:

    适用于 xcode 8.x 和 swift 3.x

    我们可以控制特定屏幕的设备方向。 在 AppDelegate.swift 上创建一个变量 enableAllOrientation:

    var enableAllOrientation = false
    
        func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            if (enableAllOrientation == true){
                return UIInterfaceOrientationMask.all
            }
            return UIInterfaceOrientationMask.portrait
        } 
    

    & 特定视图控制器的掩码为 -

    override func viewWillAppear(_ animated: Bool)
        {
            super.viewWillAppear(animated)
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.enableAllOrientation = false
        }
    

    【讨论】:

    • 我有一个带有桥接头的 Objective-c 应用程序委托,我将如何利用它?
    • @rtnlsltn 使用协议。让你的 AppDelegate 符合它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2017-01-09
    • 2022-12-20
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多