Tengo un controller Split View para iPad que debe admitir ambas orientaciones (vertical y horizontal).
El problema es que cuando cambio la orientación del dispositivo, la vista de detalles no se reajusta para usar todo el espacio y aparece un cuadrado gris.
No creo que sea un problema con las restricciones porque esto solo sucede cuando rotar:
En SplitViewController (y también en DetailsController) probé este código:
En la vistaDidLoad:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
y entonces:
override func shouldAutorotate() -> Bool { print("should rotate --> true") return true } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { print("we support all orientations") return UIInterfaceOrientationMask.All } override func prefernetworkingInterfaceOrientationForPresentation() -> UIInterfaceOrientation { print("prefernetworking orientation for presentation is landscape") return UIInterfaceOrientation.LandscapeLeft } func rotated(){ print("rotatiooooonn ------------") refreshIpad() detailSplitViewController.refreshUI() } func refreshIpad (){ dispatch_async(dispatch_get_main_queue(),{ self.view.setNeedsLayout() }) }
En los loggings obtengo esto (por lo que puedo ver cada vez que se produce el cambio de orientación)
should rotate --> true we support all orientations rotatiooooonn ------------
A continuación se muestra el file de información:
Storyboard:
Restricciones en la vista de detalles:
En refreshIpad()
necesitas llamar explícitamente a setNeedsLayout()
para todas tus subvenciones ya que no ocurre de forma recursiva,
func refreshIpad() { dispatch_async(dispatch_get_main_queue(),{ self.view.setNeedsLayout() for subview in self.view.subviews { subview.setNeedsLayout() } }) }
Solución recursiva (Swift 3.0)
extension UIView { func setNeedsLayoutForSubviews() { self.subviews.forEach({ $0.setNeedsLayout() $0.setNeedsLayoutForSubviews() }) } } func refreshIpad() { DispatchQueue.main.async { self.view.setNeedsLayout() self.view.setNeedsLayoutForSubviews() } }