问题
I was searching
that how can I change the orientation
to LandScape
mode only for spefic view controller
when the Device Orientation
is set to Portrait
in the project settings
. Didn't get any succesfull result. so finally I found a way to do it.
this is project device orientation looks like ....
In swift 2.3 add following methods
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.LandscapeLeft
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft
}
For swift 3.0 do like below ....
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeLeft
}
Note :This will work fine, if you navigate from one view to another view controller without a navigation controller .
for more see this small video tutorial : change device orientation for specific view controller in ios
if anyone has better way to do this or comments , hope your answer or comments here . thanx .
回答1:
Use supportedInterfaceOrientationsFor application delegate.add following code in AppDelegate file
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if isIpadLandscape {
return UIInterfaceOrientationMask.all;
}else{
return UIInterfaceOrientationMask.portrait;
}
}
Add particular **viewcontroller** page you want to **rotate**
override func viewDidLoad() {
super.viewDidLoad()
isLandscape = true
}
override func viewWillDisappear(_ animated: Bool) {
isLandscape = false
}
And dont forget to add global varibale in appdelegate file
import UIKit
import CoreData
import Foundation
var isLandscape = false
@UIApplicationMain
来源:https://stackoverflow.com/questions/42665450/change-the-orientation-only-for-specific-view-controllers-in-ios-swift-2-3-and-s