2

Dies ist mehr oder weniger meine Main.storyboard Situation:Swift: ist es richtig, allen UINavigationControllern eine benutzerdefinierte Klasse zuzuweisen?

enter image description here

, wo ich eine Wurzel UITabBarController mit 5 possibile Möglichkeiten. Dann möchte ich, dass einige UIViewController s zu Landschaft drehen können, während ich auch einige andere UIViewController s nur im Querformat haben möchte. Also habe ich diese file.swift geschrieben:

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate { 

    override func shouldAutorotate() -> Bool { 
    if (self.topViewController?.isKindOfClass(HomeViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(ServicesViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(PhotoGalleryViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(SelectMapViewController) != nil) {return false} 
    else if (self.topViewController?.isKindOfClass(MapViewController) != nil) {return false} 
    else {return true} 
    } 

} 

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate { 
    override func shouldAutorotate() -> Bool { 
    return (self.selectedViewController as! UINavigationController).shouldAutorotate() 
    } 
} 

und ich zugewiesen haben alle UINavigationController s die gleiche Klasse CustomNavigationController während ich CustomTabBarController Klasse UITabBarController zugewiesen haben. Das Ergebnis ist, dass kein View-Controller nicht rotiert. Liegt das daran, dass ich ihnen dieselbe Klasse zugewiesen habe? Soll ich eine benutzerdefinierte Navigationssteuerungsklasse für jede UINavigationController erstellen, die ich habe?

UPDATE

Eine partielle Lösung, die ich gefunden, auch wenn es ein wenig kompliziert ist, ist die folgende.

class CustomNavigationController: UINavigationController, UINavigationControllerDelegate { 

override func shouldAutorotate() -> Bool { 
    return (self.topViewController?.shouldAutorotate())! 
} 

} 

Klasse CustomTabBarController: Ich habe die vorherige Datei wie das modifizierte UITabBarController, UITabBarControllerDelegate { Überschreibung func shouldAutorotate() -> Bool { return (self.selectedViewController als UINavigationController!) .shouldAutorotate() } }

in view-Controller dann, wo Drehung ich einfach erlaubt ist, haben:

override func shouldAutorotate() -> Bool { 
    return true 
} 

, während in View-Controller, wo Rotation nicht erlaubt ist, ich habe:

override func shouldAutorotate() -> Bool { 
    return false 
} 

override func viewWillAppear(animated: Bool) { 
    let value = UIInterfaceOrientation.Portrait.rawValue 
    UIDevice.currentDevice().setValue(value, forKey: "orientation") 
} 

Auf jeden Fall gibt es ein kleines Problem, weil die Animation, den Modus auf Hochformat setzt nicht korrekte Bedeutung ist, dass die Breite des Bildschirms nicht eingestellt wird. Wenn ich von einem Querformat-Controller zu einem Nur-Portrait-Controller gehe, ist der View-Controller-Frame nicht korrekt. Ich bekomme

enter image description here

statt dessen:

enter image description here

Antwort

1

Versuchen Sie dies in AppDelegate

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { 
    if let rootController = self.window?.rootViewController as? UITabBarController { 
     if let navigationController = rootController.selectedViewController as? UINavigationController { 
      let controller = navigationController.topViewController! 

      if controller.isKindOfClass(HomeViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(ServicesViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(PhotoGalleryViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(SelectMapViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
      if controller.isKindOfClass(MapViewController.classForCoder()) { 
       return UIInterfaceOrientationMask.Portrait 
      } 
     } 
    } 
    return UIInterfaceOrientationMask.All 
} 

Update:

Diese Methode Kräfte Anwendungsorientierung in ViewCo ändern ntroller, die nur im Hochformat (HomeViewController, ServicesViewController, PhotoGalleryViewController, SelectMapViewController, MapViewController) sein sollte:

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
    let value = UIInterfaceOrientation.Portrait.rawValue 
    UIDevice.currentDevice().setValue(value, forKey: "orientation") 
} 
+0

Vielen Dank für Ihren Vorschlag, aber ich fürchte, es nicht funktioniert ... sie alle drehen ohne Ausnahme :( –

+0

@LoryLory tut mir leid, ich habe meine Antwort bearbeitet.Previous arbeitete nur mit Präsentiertem Controller. Dieser sollte funktionieren. Es funktioniert auch ohne CustomNavigationController. Probieren Sie es;) –

+0

ok es funktioniert, aber jetzt habe ich das Problem in meinem Frage-Update beschrieben: Wenn ich von einem Querformat-View-Controller zu einem Portrait nur Ansicht-Controller gehen, dann ist der View-Controller-Frame nicht korrekt. Sehe die neuen Bilder. –