Question

I've noticed that most consumer-friendly Android and iPhone fitness apps have two interface modes - in portrait mode the user gets more detailed information, but when the user turns the device to landscape mode, a full screen graph is added to cover the entire screen.

I'm interested in how to implement transition to a different view controller in response to device rotation on iPhone. My initial thoughts are to intercept (willRotateToInterfaceOrientation event, then get the app delegate and add a full screen graph view controller to the window).

Is there a better way of turning an iPhone rotation into a transition to another view controller? Like hiding the status bar and pushing a modal view controller in landscape mode with animation?

Was it helpful?

Solution

First ask yourself whether you really need a separate view controller. One view controller can easily hide or unhide a graph. If this graph needs its own view conroller then you could use a container view that contains the graph which refers to its own view conroller. That is what container views are made for. The "Master" view controller then would just hide and unhide the container view in response to rotation events (and layout them accordingly etc. pp.)

If you prefer to add or remove the container view from its super view (most probably self.view from the "Master" view controller's point of view) then do that instead of hiding and unhiding. That is probably most appropriate.

The upside of this appoach would be that it works regardless of the navigaiton structure you are in, regardless of whether the rotated view controller was pushed or presented modally, regardless of whether you are in a tab bar driven app or a single view app, whether you are using storyboard, works with IB as well as programmatically, etc. pp.

There is nothing wrong with fetching the window instance from the app's delegate. I just don't see the need for doing so. Seems rather complicated to me compared to the alternatives.

OTHER TIPS

The willRotateToInterfaceOrientation method works well.

In addition to switching views, two other useful things you might want to do in there are:

1) Hide/Show the status bar. (I like to hide it in landscape)

[[UIApplication sharedApplication] setStatusBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation)  withAnimation:UIStatusBarAnimationSlide];

2) Hide/Show any UINavigationBar. (Maybe your landscape view will benefit from the extra height)

[self.navigationController setNavigationBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation) animated:YES];

You could have one view controller that has the willRotateToInterfaceOrientation method, and that viewcontroller has two other viewcontrollers as variables.

Once the device rotates, you switch the viewcontrollers' views (very crude code example:)

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {

if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
    [self.secondViewController.view removeFromSuperView];
    self.firstViewController.view.frame = self.bounds;
    [self.view addSubView:self.firstViewController.view];
  } else {
    [self.firstViewController.view removeFromSuperView];
    self.secondViewController.view.frame = self.bounds;
    [self.view addSubView:self.secondViewController.view];
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top