특정 ViewController 의 orientation 변경

HS Lee·2022년 4월 21일
0

iOS

목록 보기
2/2

2018-01-14에 작성

Change orientation a specific viewController

  • Set the orientation option on project settings
  • In First, Set the orientation at AppDelegate
var enableAllOrientation = false
  func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
      if (enableAllOrientation == true){
          return UIInterfaceOrientationMask.allButUpsideDown
      }
      return UIInterfaceOrientationMask.portrait
  }
  • Enable rotating orientation onto a specific viewController
override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

    JDFacade.app.enableAllOrientation = true
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    let value = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")

    JDFacade.app.enableAllOrientation = false
}
  • when back to the non-orientation viewController
  override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)

      let rect = CGRect(x: 0, y: 0, width: 375, height: 667)
      if let wrapperView = self.tabBarController?.view.subviews[0].subviews[0] {
          wrapperView.frame = rect
      }

      UIViewController.attemptRotationToDeviceOrientation()
  }
profile
software engineer

0개의 댓글