I am porting an iPad app in iOS8 and Swift.
In the portrait, I use the root UIViewController, and when the device turns to landscape, I switch to another UIViewController. I came up with 2 solutions, one based on UIDevice notifications, and the other based on willRotateToInterfaceRotation . I always try to stay away from the Observer pattern, it's just a habit for me.
The observer works fine, but redefinition in both UIViewController func willRotateToInterfaceOrientation (toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) looks cleaner for my eyes;)
But now in iOS8, this feature is deprecated, and I have to use
func viewWillTransitionToSize(_ size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
But I do not know how I can work with this to get the same result.
This is rootViewController :
override func willRotateToInterfaceOrientation( toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { if (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight) { self.performSegueWithIdentifier("toLandscape", sender: self) } }
and UIViewControllerLanscape :
override func willRotateToInterfaceOrientation( toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { if (toInterfaceOrientation == UIInterfaceOrientation.Portrait || toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown) { self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil) } }
I do not like to use obsolete functions, so I doubt what to do ... go for the observer or what?
This is the code I use when the (only) root UIViewController is a UIDeviceOrientationDidChangeNotification observer:
override func awakeFromNib() { let dev = UIDevice.currentDevice() dev.beginGeneratingDeviceOrientationNotifications() let nc = NSNotificationCenter.defaultCenter() nc.addObserver(self, selector: "orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: dev) } func orientationChanged(note: NSNotification) -> () { if let uidevice: UIDevice = note.object? as? UIDevice { switch uidevice.orientation { case .LandscapeLeft, .LandscapeRight: self.performSegueWithIdentifier("toLandscape", sender: self) default: self.dismissViewControllerAnimated(true, completion: nil) } } }
I really like your ideas on how to make a decision for these deprecated features. This is a complete secret for me .... or maybe Observer is the best solution now. Share your ideas.
Hi,
jr00n