6월 21일 (화)

apwierk·2022년 6월 23일
0

TIL

목록 보기
22/33

TIL (Today I Learned)

6월 21일 (화)

학습 내용

Device Orientation

기기의 방향 고정하기

  • All (고정 안함)
  • Portrait (세로모드)
  • landscape (가로모드)
  • Upside Down (위아래 반전)

위 타입은 UIInterfaceOrientationMask 타입이다.

AppDelegate파일 내부에 다음 메서드를 추가해준다.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
	return UIInterfaceOrientationMask.all
    // 점"." 뒤에 portrait를 설정할 경우 모든 화면이 세로모드로 고정된다.
}

View 마다 기기의 방향 다르게 설정하기

  1. AppDelegate에 프로퍼티와 메서드를 추가
var orientationLock = UIInterfaceOrientationMask.all // 기본 값 고정 안함으로 설정

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            return self.orientationLock
}

2.고정해야 될 뷰의 viewWillAppear에서 orientationLock 프로퍼티의 값을 변경 시켜준다.
3.viewWillDisappear에서 다시 default였던 .all로 변경 시켜준다.

override func viewWillAppear(_ animated: Bool) {
	super.viewWillAppear(animated)
	if let delegate = UIApplication.shared.delegate as? AppDelegate {
		delegate.orientationLock = .portrait
	}
}
override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
		delegate.orientationLock = .all
	}
}

참고

DeviceOrientation

profile
iOS 꿈나무 개발자

0개의 댓글