菜鸟教程小白 发表于 2022-12-11 20:47:20

ios - 默认纵向,一些特殊屏幕如何自动旋转或强制某些屏幕横向?


                                            <p><p>在我的应用中,方向默认为纵向,但在某些特殊屏幕中,我希望 ViewController 自动旋转或强制方向为横向。</p>

<p>在 <strong>AppDelegate.swift</strong> 中:</p>

<pre><code>class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

// portrait by default
var interfaceOrientations:UIInterfaceOrientationMask = .portrait {
    didSet{
      // force to portrait
      if interfaceOrientations == .portrait {
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
                                    forKey: &#34;orientation&#34;)
      }
            // force to landscape
      else if !interfaceOrientations.contains(.portrait){
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,
                                    forKey: &#34;orientation&#34;)
      }
    }
}

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -&gt; UIInterfaceOrientationMask {
    return interfaceOrientations
}
</code></pre>

<p>仅在纵向 vc 中:</p>

<pre><code>override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    appDelegate.interfaceOrientations = .portrait
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
}
</code></pre>

<p>在自动旋转的vc中:</p>

<pre><code>override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    appDelegate.interfaceOrientations = .allButUpsideDown
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
}
</code></pre>

<p>仅在横向 vc 中:</p>

<pre><code>override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
}
</code></pre>

<p>但它仍然有错误...帮助我</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你应该强制旋转:</p>

<pre><code>UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: &#34;orientation&#34;)
</code></pre>

<p>您还必须启用自动旋转</p>

<pre><code>override var shouldAutorotate: Bool{
    return true
}
</code></pre>

<p>当然,在您的项目设置中,您必须同时启用纵向和横向模式。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 默认纵向,一些特殊屏幕如何自动旋转或强制某些屏幕横向?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/53205704/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/53205704/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 默认纵向,一些特殊屏幕如何自动旋转或强制某些屏幕横向?