仅在ios swift 2.3和swift 3中更改特定视图控制器的方向 [英] Change the orientation only for specific view controllers in ios swift 2.3 and swift 3

查看:63
本文介绍了仅在ios swift 2.3和swift 3中更改特定视图控制器的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是searching,当项目settings中的Device Orientation设置为Portrait时,如何仅将spefic view controllerorientation更改为LandScape模式.没有获得任何成功的结果.所以最后我找到了一种方法.

I was searching that how can I change the orientation to LandScape mode only for spefic view controller when the Device Orientation is set to Portrait in the project settings. Didn't get any succesfull result. so finally I found a way to do it.

这是项目设备方向,看起来像....

this is project device orientation looks like ....

在Swift 2.3中添加以下方法

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

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


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.LandscapeLeft
    }

对于Swift 3.0,请执行以下操作....

  override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeLeft
    }

    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeLeft
    }

注意:如果您在没有导航控制器的情况下从一个视图导航到另一个视图控制器,这将很好地工作.

有关更多信息,请参见此小型视频教程:更改ios中特定视图控制器的设备方向

for more see this small video tutorial : change device orientation for specific view controller in ios

如果有人有更好的方法来进行此操作或发表评论,请在此处希望您的回答或评论. thanx.

if anyone has better way to do this or comments , hope your answer or comments here . thanx .

推荐答案

使用 supportedInterfaceOrientationsFor 应用程序委托.在 AppDelegate 文件中添加以下代码

Use supportedInterfaceOrientationsFor application delegate.add following code in AppDelegate file

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

            if isIpadLandscape {
                 return UIInterfaceOrientationMask.all;
            }else{
                return UIInterfaceOrientationMask.portrait;

            }


    }


Add particular **viewcontroller** page you want to **rotate**

     override func viewDidLoad() {
            super.viewDidLoad()
            isLandscape = true
    }
    override func viewWillDisappear(_ animated: Bool) {
            isLandscape = false
        }

别忘了在appdelegate文件中添加全局变量

And dont forget to add global varibale in appdelegate file

import UIKit
import CoreData
import Foundation
    var isLandscape = false
    @UIApplicationMain

这篇关于仅在ios swift 2.3和swift 3中更改特定视图控制器的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆