Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPad/iPhone multiple orientations best practice?

I'd like to convert my views to work for any orientation (especially since it recommended for iPad). I've been using IB to lay things out, and am not sure how to best proceed.

What I would ideally like is to rotate the view in IB, redo the layout, and save both orientations to the same XIB so that the view automatically knows what to do when the orientation changes. This doesn't seem possible.

I know I can rearrange the views in code when the orientation changes, but then there's not much point in using IB, since one of its main advantages for me has been to separate out all that ugly layout code from my logic.

What do others do for this? Do they just design their views so that the UIViewAutoResizing flags can handle rotations appropriately? Do they have multiple views for each orientation and somehow switch these out smoothly?

like image 562
initlaunch Avatar asked Dec 28 '22 04:12

initlaunch


2 Answers

I found the information here (and elsewhere on StackOverFlow on this topic), and it got me to thinking about my previous answer. This just gets tedious if you have lots of top level objects.

Then, it occurred to me that you could create a "helper" object, so I created a project to demonstrate the technique. Basically, the helper has an outlet for each object of interest in the view. The ViewController itself has two Helper objects (one for p, one for l) and you instantiate those in the NIB. On orientation changes, you switch the view controller's helper pointer to the appropriate helper, then update self.view. Its really simple (this text is harder).

So, how would you start? Well it helps if you know a priori that you will need two separate view. Once you know that, take the Helper template in the referenced project below, adapt it, add it to your NIB, and then connect the help to the appropriate objects in the portrait view. The View Controller itself just has a reference to the two helpers, and a "curHelper" pointer. The Helper ivars are public, so the View Controller can reference items like "curHelper->label1.text = ... - the cost is a single pointer dereference (could just use properties too - your call).

Also, you can directly connect IBActions from the objects in either view. So, lets walk through this:

  • create the NIB
  • create a Helper object, and instantiate one in the nib called Portrait
  • add all the IBOutlets to the Helper object, handle then in dealloc, viewWillUnload
  • put IBActions as normal in the View Controller
  • wire up the NIB - Helper->Portrait view outlets, actions to the View Controller
  • get it all working in Portrait
  • add a new Helper object, call it Landscape
  • dup the current portrait view, rotate it, and save it
  • wire up the landscape Helper's outlets to the new landscape view (actions already set for you!)

Obviously you need to from now on make duplicate changes, but in any dual nib scheme you would have to do that. In the above technique, all outlet names stay the same.

The View Controller needs to send "viewDidUnload" to both helpers when it receives it, and to dealloc the Helpers (which then dealloc their outlets).

NOTE: I just put this together to see how it looked. If you want to see the code and a really small demo, you can download it from public dot me dot com slash dhoerl, in the iPhone folder with name Helpers.zip. I'll update this and this text if needed.

like image 104
David H Avatar answered Jan 11 '23 21:01

David H


You can make two separate views in the same XIB, and then implement the following code:

To @interface

IBOutlet UIView *landscapeView;
IBOutlet UIView *portraitView;

and to @implementation

- (void) viewDidLoad {
self.view.frame = [[UIScreen mainScreen] applicationFrame];
  landscapeView.autoresizesSubviews = NO;  <- IMPORTANT!
  portraitView.autoresizesSubviews = NO;   <- IMPORTANT!
}
- (BOOL)shouldAutorotateToInt .... {
  if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight) self.view = landscapeView;
  else if ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) self.view = portraitView;
  return YES;
}
like image 28
Fred Avatar answered Jan 11 '23 20:01

Fred