在我的项目中,单独将单个视图控制器的方向更改为两者 风景和肖像。
以下是我尝试更改视图控制器方向的代码
@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
}
}