RotatingViewController: Display a different UIViewController depending on the orientation of the device

Autor: | Última modificación: 14 de noviembre de 2022 | Tiempo de Lectura: 1 minutos
Temas en este post:

RotatingViewController

Sometimes you may want to display a different view in landscape and portrait mode. A good example is Apple’s Calculator: it switches from normal in portrait mode to scientific in landscape mode.

There are several reasons you may want to do so:

  1. Your view is very complex and it’s impossible or very difficult to reuse it in landscape mode.
  2. You wish to use the extra horizontal space on the landscape mode to display new features. Apple’s Calculator app is a good example.
  3. You are using a sub view that takes up too much space in landscape, such as a UIPickerView.
Another app that uses this in an interesting way is WeighBot, which displays different graphics depending on the orientation.
I’m not sure which solution TapBots or Apple used for this. Mine was to create a new controller of controllers (a controller that controls other controllers),  inspired by UINavigationController and friends.

FRRRotatingViewController

FRRRotatingViewController will handle the VC switching _automagically_ for you: just initialize it with 2 FRRViewControllers
and it will do the magic for you.

[crayon lang="Objective C"]
-(FRRRotatingViewController *) initWithLandscapeViewController: (FRRViewController *) landscape
andPortraitViewController: (FRRViewController *) portrait;
[/crayon]

In the images below, a FRRRotatingController switches between two simple VCs that display an image:

RotatingViewControllerLandscape

Both sub view controllers can be delegated and be notified when the FRRRotatingViewController switches sub view
controllers.

Source Code

The source code is available at GitHub.