개발노트/iOS
SWIFT : 특정 뷰 화면 전환하기 - 세로모드 예제(orientation : portrait)
전지적진영시점
2023. 2. 20. 10:23
이번 포스팅은 특정 뷰 컨트롤러에서 세로모드 고정을 알아보겠습니다.
전체 뷰를 한가지 방향으로 고정하는 방법도 있지만
특정 뷰만 세로모드로 고정되어 로드되어야 할 때가 있습니다.
✌️ 세로모드로 고정 로드되어야 할 뷰 컨트롤러 안에 아래의 소스를 넣어주세요
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var shouldAutorotate: Bool {
return true
}
supportedInterfaceOrientations
해당 메서드는 지원되는 인터페이스의 방향을 반환합니다
shouldAutorotate
콘텐츠가 자동회전해야 하는지 여부를 묻는 Bool 값입니다
supportedInterfaceOrientations 값을 landscape 혹은 portrait 로 줘도 shouldAutorotate 값이 true면 화면이 자동회전 가능합니다. |
✌️ 소스를 작성하신 후에 ViewDidLoad() 메서드 안에 아래의 소스를 넣어주세요
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
전체 소스 코드
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: url!)
self.webView.allowsBackForwardNavigationGestures = true //2021.12.30 [jylee] 뒤로가기 제스쳐 허용
webView.configuration.preferences.javaScriptEnabled = true //2021.12.30 [jylee] 자바스크립트 활성화
webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = true
webView.load(request)
webViewContainer.addSubview(webView)
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var shouldAutorotate: Bool {
return false
}
빌드하면 세로 고정 모드로 빌드된 앱을 확인할 수 있습니다😍
>>>>>추가
🎃 지원하는 Orientation에 대해 이해하기 쉽게 설명하자면!
Portrait : 세로 모드 (정방향)
UpsideDown : 세로 모드 (역방향 : 상하 반전)
LandScapeLeft : 가로모드 (홈 버튼이 왼쪽)
LandScapeRight : 가로모드 (홈 버튼이 오른쪽)
필요에 따라서 적절히 넣어주시면 됩니다!
그럼 오늘도 좋은 하루 되세요~!(งᐖ)ว