WillRotateToInterfaceOrientation is deprecated but does not understand viewWillTransitionToSize: withTransitionCoordinator:

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

+7
ios uiviewcontroller
source share

No one has answered this question yet.

See related questions:

497
UIDevice uniqueIdentifier is deprecated - what to do now?
265
Looking for an understanding of the life cycle of iOS UIViewController
thirteen
willRotateToInterfaceOrientation is not called
4
UIView rotation - willRotateToInterfaceOrientation not called
3
iOS willRotateToInterfaceOrientation proper use
3
UIModalPresentationCurrentContext and willRotateToInterfaceOrientation
2
Cocos2d Not Viewed Correctly in UISplitViewController
one
iOS 9 will be inactive
one
Swift error - using cell of implicit type 'cell' - Collection View
0
NavigationController willRotateToInterfaceOrientation is not called if I change the topview control orientation in iOS 8

All Articles