Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best method to support multiple orientations on iPAD with storyboards (no AutoLayout or AutoResizing)

I want to support landscape & portrait orientations on my iPAD app that uses a storyboard without getting involved into the intricacies of Auto Layout on iOS6+ and Auto Resizing on iOS 5 and earlier (since the app will support both iOS 5 & 6 so no AutoLayout is allowed here), what I thought of as a beginning point for the solution was the following:

creating two separate storyboards: MainStoryboard-Portrait & MainStoryboard-Landscape, when the current view controller (let's name it FirstViewController) is in portrait, and the user rotates the device to landscape, I instantiate a new FirstViewController from MainStoryboard-Landscape storyboard, and vice versa when the user rotates back to portrait. I did something like this in willRotateToInterfaceOrientation method in FirstViewController.m :

  UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"MainStoryboard-Landscape" bundle:[NSBundle mainBundle]];
  FirstViewController *VC =  [storyboard instantiateViewControllerWithIdentifier:@"VC1"];
  self.view = VC.view;

but the following crash happens on iOS6+: A view can only be associated with at most one view controller at a time!, tried it also on iOS5 there will be no crash but the rotation does not work properly: window bounds rotates but the view itself keeps as is.

How to get this working on both iOS 5 & 6 ? or if there is another better method please provide me with a working code sample for it and i will award you a bounty of 50 points.

like image 462
JAHelia Avatar asked Dec 30 '12 11:12

JAHelia


2 Answers

if you really don't wish to use auto layout, doing this in a single .storyboard can still be done by having an IBOutlet (weak, nonatomic) UIView* portraitView and an IBOutlet (weak, nonatomic) UIView* landscapeView. create each view as a subview of the main view for FirstViewController in .storyboard.

then in willRotateToInterfaceOrientation:, perform the following:

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        self.portraitView.hidden = NO;
        self.landscapeView.hidden = YES;
    }
    else
    {
        self.portraitView.hidden = YES;
        self.landscapeView.hidden = NO;
    }

when working on your views in .storyboard, you can see each subview a little better by checking/unchecking the hidden flag in the right sidebar.

then, another advantage of this is that if you have some views that look ok regardless of orientation, you don't have to maintain a separate storyboard file and scene for them.

like image 196
john.k.doe Avatar answered Sep 19 '22 18:09

john.k.doe


Auto layout really is the way to go. As soon as you've got two storyboards you've got a maintenance and consistency headache — if you want the portrait and landscape user experience to be the same. If you specifically want them to be different for a reason, then two storyboards is appropriate.

like image 45
Andrew Avatar answered Sep 20 '22 18:09

Andrew