Create single .xib for Universal app in Interface Builder? (iOS)

WendiKidd picture WendiKidd · Apr 5, 2012 · Viewed 8.9k times · Source

Apologies if this is a silly question, but I've done some googling and searched SO and haven't found anyone asking this exact question.

I have been doing iOS development for some time now, but I am completely new to the Interface Builder. What I want to know is this: is there any way to just create ONE .xib file and then use it for both iPhone and iPad in a Universal application?

It seems silly to me to have to create them separately; why do twice the work laying something out more than once in Interface Builder when I could do it once (with minor adjustments for screen size) in code?

Please let me know if I'm missing/misunderstanding something here. Like I said, I'm a complete Interface Builder newbie :)

EDIT: I have submitted non-interface-builder games to the App Store in the past where the iPhone and iPad versions were identical, so I'm not concerned with making the game look/feel different on each device. I intend for them to look exactly the same, aside from some slight positioning changes due to the difference in aspect ratio.

Answer

themarketka picture themarketka · Apr 5, 2012

If you know what the resulting view would look like, based on autoresizing, you can indeed use only one .xib. May come in handy if the view is just some sort of a shared component that autoresizes as you want it to. However, if you need the view to look way different on iPad than on iPhone, just use two .xibs. It’s possible then to load the appropriate one as needed, for example in instance initializer, like this controller’s -init:

- (id)init
{
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
         self = [super initWithNibName:@"YourNibForPad" bundle:nil];
    }
    else
    {
         self = [super initWithNibName:@"YourNibForPhone" bundle:nil];
    }
    if (self) { /* initialize other ivars */ }
    return self;
}