Swapping ViewControllers on device rotation

I recently needed to have one ViewController for portrait orientation and a different ViewController for landscape. To achieve this using iOS 8 and Swift, create the two ViewControllers in your StoryBoard along with their classes. Ctrl-drag form one to the other and vice-versa, creating modal segues between them. Name the Identifiers; in my case these were fromMap and fromNews. Then, in each of the ViewController classes, override willRotateToInterfaceOrientation(…).

 
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)
{
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
        {
                self.performSegueWithIdentifier("fromNews", sender: self)
        }
}
 
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)
{
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
        {
                self.performSegueWithIdentifier("fromMap", sender: self)
        }
}

One thought on “Swapping ViewControllers on device rotation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s