Question

In a certain scenario, I wish to replace the presented Nib for a View Controller upon Device Orientation Change.

Something like this:

enter image description here enter image description here

I wish for both of the Nib files to be owned by the same View Controller, so they react to the same IBActions, IBOutlets, etc...

Also, Auto Layout will be a bit complicated for this specific scenario (the images I've used here are just to demonstrate the issue - the actual implementation is more complicated...).

Apple example for this scenario is problematic as it uses a Segue for this action. If, for example, the VC is part of a Navigation Controller stack, it will make it very problematic to react to the Back button click - instead of just Popping the VC I will have to Pop 2 VCs (I think):

enter image description here

Intuitively, I'm thinking about replacing the Nib file (and not performing a Segue).

Is it a valid action?

If so, how should I perform it in code?

Also, can it be achieved in Storyboard, or do I need to use a .xib file?

Was it helpful?

Solution

Autolayout si perfect for this scenario, the "only" problem is that you can't do in Storyboard Editor, but in code. I really suggest you a book there is an equal problem Erica Sadun Autolayout demistified.
The approach is to have different sets of constraints, one for landscape and one portrait, removing and installing them alternatively, but maybe the best would be just change their priorities.
Changing the xib seems an easier solution, but in my opionio is a less maintinable options.
You should:

  • create 2 xibs: one for portrait an one for landscape
  • on the view controller's xib, create a container view, with autoresizing masks or constraint that will stretch it according to main view changes.
  • in the view controller method - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration swap the 2 view. since this is wrapped in an animation block, you can just lower alpha on one a turn on on the other and remove

Take a look a here

OTHER TIPS

You can do something like this :

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(devOrientation) && !self.viewIsLandscape ) {
        [self presentModalViewController:self.landscapeVC animated:YES];
        self.viewIsLandscape = YES;
    } else if (UIDeviceOrientationIsPortrait(devOrientation) &&
               self.viewIsLandscape)
    {
        [self dismissModalViewControllerAnimated:YES];
        self.viewIsLandscape = NO;
    }
}

Ref : http://www.edumobile.org/iphone/iphone-programming-tutorials/orientation-changes-for-iphone/

Hope this will help you...

You can create two separate UIViews inside single XIB instead of two different xibs. And you can change the view based on change of orientation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top