티스토리 뷰

AppDelegate 에서


class AppDelegate: UIResponder, UIApplicationDelegate {


.....


 var shouldSupportAllOrientation = true

 //화면회전을 잠그고 고정할 목적의 플래그 변수를 추가한다.


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

        if (shouldSupportAllOrientation == true){

            return UIInterfaceOrientationMask.all

//  모든방향 회전 가능

        }

        return UIInterfaceOrientationMask.portrait

//  세로방향으로 고정.

    }


   .....


}



위와 같이 설정 후

화면을 고정하고자 하는 view controller 에서


let appDelegate = UIApplication.shared.delegate as! AppDelegate


override func viewWillAppear(_ animated: Bool) {

    appDelegate.shouldSupportAllOrientation = false

}


override func viewWillDisappear(_ animated: Bool) {

    appDelegate.shouldSupportAllOrientation = true

}


해주면 된다.

* viewWillDisappear 에서 true로 해주는 것을 깜빡할경우

이후 모든 뷰컨트롤러에서 화면이 고정되므로 각별히 주의해야함.





참고.

UIInterfaceOrientationMask 의 설정값은 아래와 같다.

의도에 맞게 설정하면 됨.


.all (모든방향)

.allButUpsideDown (세로뒤집기 빼고 모든방향)

.landscape (가로)

.landscapeLeft (가로 왼쪽만)

.landscapeRight (가로 오른쪽만)

.portrait (세로. 뒤집기 불가)

.portraitUpsideDown (뒤집기 포함한 세로)



공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함