How to Push a New UIViewController on Device Orientation Change


At this point I’m sure everyone has seen the iOS Music app’s album view—where you tilt your phone horizontally and the screen suddenly displays all of the album art on your phone.

While viewing a collage of cover art always felt a little useless to me, I always thought the effect was pretty cool and wanted to do something similar in the music app I’m currently working on. As such, I took to google and spent longer than I’d care to admit trying to figure this one out. In the process I got an astonishing variety of errors, but once I finally figured it out the process was actually really simple and not a lot of code at all.

Basically, the code is broken into two methods. The first goes in the vertical ViewController:

and the second in the horizontal:

Here, the first method (the one in the vertical ViewController) is performing a manual segue based on the device’s orientation state. The second method is basically doing the reverse. It dismisses said ViewController, but does so after the UI has had a moment to reset itself to vertical (you get some pretty gnarly errors if you try and segue back before allowing the UI to finish changing orientation). The end result is an effect very similar to the one in the iOS music app. There are a couple of more things to note however:

  1. You must have at least one Landscape option enabled on your app for this to work, otherwise the app simply won’t rotate.
  2. You have to place the vertical VC code in every single vertical ViewController you want to enable the segue functionality from.

All in all it’s pretty simple. I’m sure there are a couple of better ways out there to get a similar effect (it’s frustrating to have to copy/paste the same method into multiple VCs), but this one seemed easier than most alternatives I could come up with. Anyway, I hope this helps anyone that may be looking to use a similar effect in their apps. Used correctly, I feel like it’s a great way to present useful information to a user.

2 thoughts on “How to Push a New UIViewController on Device Orientation Change”

  1. holt says:

    Hey there! I’ve been following your site for some time
    now and finally got the bravery to go ahead and give you
    a shout out from Lubbock Tx! Just wanted
    to say keep up the fantastic job!

  2. stój says:

    Keep this going please, great job!

Leave a Reply

Your email address will not be published. Required fields are marked *