# 개발자노트/IOS

[Swift]특정 화면에서만 가로 모드 지원

쩡냥이 2020. 7. 27. 11:04

* 한화면에서만 가로 모드 지원 하기

 

플레이어 등 한 화면만 가로모드로 지원했다가 다른 화면은 세로로 고정 되도록 해야 할 때가 있다.

먼저 AppDelegate.swift에 다음 코드를 추가한다. 다른 화면들은 세로로 고정 할려고 Lock은 portrait로 고정 시켜놓았다.

var orientationLock = UIInterfaceOrientationMask.portrait

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

        return self.orientationLock

    }

    

    struct AppUtility {

        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {

            if let delegate = UIApplication.shared.delegate as? AppDelegate {

                delegate.orientationLock = orientation

            }

        }

        

        static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {

            self.lockOrientation(orientation)

            UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")

        }

    }

    

 

그리고 전환하는 화면 맨위에 Delegate를 선언해 준다

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

 

그리고 ViewWillAppear안에 다음과 같이 방향을 정해준다

AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)

 

그리고 돌아오는 화면에는 다시 Lock을 세로로 고정 해줘야 하기 때문에 다음과 같이 선언해줬다.

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait)

 

까먹고기 방지피드 !!!