在iOS swift中将VIewController旋转为横向或纵向

时间:2017-12-27 12:54:33

标签: ios swift xcode swift3 swift4

在我的项目中,单独将单个视图控制器的方向更改为两者   风景和肖像。

以下是我尝试更改视图控制器方向的代码

@IBOutlet weak var imagePhoto:UIImageView!

left.addEventListener("click", function(){ plusDivs(-1) }); right.addEventListener("click", function(){ plusDivs(1) } ); 中添加:

viewDidLoad

Appdelegatedata中添加的代码

  NotificationCenter.default.addObserver(self, selector: 
  #selector(viewWillTransition(to:with:)), name: 
  NSNotification.Name.UIDeviceOrientationDidChange, object: nil)


  override func viewWillTransition(to size: CGSize, with coordinator: 
  UIViewControllerTransitionCoordinator){
     if UIDevice.current.orientation.isLandscape {
        print("Landscape")
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
           appDelegate.orientationLock = .landscape

    } else {
        print("Portrait")
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.orientationLock = .portrait


     }
   }

0 个答案:

没有答案