How to load different XIBs for different device orientations for the same monitor?

The documentation says that if I want to support both portrait and landscape, I basically have two ways to do this:

  • Customize the viewcontroller view so that subviews automatically perform automatic adjustments and make small changes programmatically at runtime
  • If the changes are more significant, create an alternative landscape interface and click / place an alternative modal view controller at runtime

I would like to provide information where the layout is significantly different, but the logic is the same. Ideally, I would download another XIB for the same view manager, but it doesn't seem to be an option.

It seems like # 2 is what I need to do, but my problem is that it looks like it will use standard modalviewcontroller animations that are not like device rotation animations. (Of course, being the lazy weber that I am, I did not test this hypothesis.)

So, how do I load an alternate landscape layout with the same view manager but with a different XIB? Should I use method # 2 above and is the rotation animation natural? Or is there some other way?

+6
iphone layout rotation uiviewcontroller
source share
1 answer

I create UIView UIView in -viewDidLoad: and add them as subtasks to the view controller property:

 - (void) viewDidLoad { [super viewDidLoad]; self.myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 210.0f)] autorelease]; // ... [self.view addSubview:myView]; } 

Then I call -viewWillAppear: to center these subzones:

 - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]]; } 

I also override -willRotateToInterfaceOrientation:duration:

 - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration { [self adjustViewsForOrientation:newInterfaceOrientation]; } 

The -adjustViewsForOrientation: method -adjustViewsForOrientation: sets the CGPoint center for various subview objects depending on the orientation of the device:

 - (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation { if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { myView.center = CGPointMake(235.0f, 42.0f); // ... } else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { myView.center = CGPointMake(160.0f, 52.0f); // ... } } 

When the view controller is loaded, UIView instances are created and placed based on the current orientation of the device. If the device is subsequently rotated, the points of view are redirected to the new coordinates.

To make this smooth, you can probably use the key animation in -adjustViewsForOrientation: so that the subview moves more gracefully from one center to another. But at the moment it works for me.

+1
source share

All Articles