Ich habe 2 View Controller, VC1 und VC2.So sperren Sie die Ausrichtung für das iPhone für bestimmte View Controller - Swift?
VC1 präsentiert derzeit VC2 modal.
VC1 nur Orientierung sollte im Hochformat sein, aber VC2 kann alle Orientierungen haben.
Das Problem ist, wenn ich in VC2 bin und ich in Querformat drehen und dann entlassen, VC1 ist auch im Querformat-Modus! Das sollte nie passieren!
HINWEIS: Es gibt keine Navigation Controller oder UITabBarController
ich meinen Code unten hinzufügen.
Appdelagate:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
if (rootViewController.responds(to: Selector(("canRotate")))) {
// Unlock landscape view orientations for this view controller
return .allButUpsideDown
}
}
// Only allow portrait (standard behaviour)
return .portrait;
}
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
if (rootViewController == nil) { return nil }
if (rootViewController.isKind(of: (UITabBarController).self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
} else if (rootViewController.isKind(of:(UINavigationController).self)) {
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
} else if (rootViewController.presentedViewController != nil) {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController
}
-Code in VC2:
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}
func canRotate() -> Void {}
Link, wo ich um Hilfe ging und fand diesen Code Website where I found Code
Vielen Dank für die Hilfe!
Sie machen das alles falsch. Wo sind Ihre Implementierungen von ['supportedInterfaceOrientations'] (https://developer.apple.com/reference/uikit/uiviewcontroller/1621435-supportedinterfaceorientations)? Beide View-Controller müssen einen haben. – matt
Ich habe das zur Zeit nicht irgendwo, weil ich es versucht habe und es scheint nicht auf iOS 10 zu arbeiten? Kannst du bitte ein wenig näher ausführen? Sehr neu dazu! Ich habe einen Link zu der Frage hinzugefügt, die zeigt, wo ich diesen Code gefunden habe – user7097242
'supportedInterfaceOrientations' war der bevorzugte Mechanismus für die Handhabung der Geräte-Rotation seit iOS 6 oder so. Sie erwähnen nicht, wie VC1 präsentiert wird, aber es scheint wahrscheinlich, dass es diese Methode überschreiben sollte, um die Rotation zu steuern. –