7

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.

WendiKidd
  • 4,333
  • 4
  • 33
  • 50

4 Answers4

10

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;
}
themarketka
  • 672
  • 6
  • 14
  • Can you explain further, please? I understand how to load the files separately, but I would like to not have to. When I create a new view-based application, it automatically creates a separate xib for iPad and iPhone. How can I just use one and link it to both devices? Thanks! – WendiKidd Apr 05 '12 at 15:28
  • 1
    If you really, really, really need that, just delete the other xib and keep the one you will need, and pass its name as parameter to `-initWithNibName:bundle:`. Also make sure that the view loaded from that xib will behave well on both devices, like autoresizing rules and so on. – themarketka Apr 06 '12 at 11:12
  • Brilliant! This is exactly what I was looking for; I now feel a bit silly for not figuring it out myself. Thanks very much for your answer! This was what I needed :) – WendiKidd Apr 06 '12 at 12:46
  • @ThePablick, This is not working as I am having different targets for ipad and iphone in the same application. any workaround for this? – Heena Apr 04 '13 at 06:45
  • 2
    You can remve the userInterfaceIdiom check by naming your pad and phone xib as follows. YourNib~ipad yourNib~iphone . Then you can do super initWithNibName:@"yourNib" and iOS will figure out which one to load for you. Then you can do.... -(instancetype)init { self = [super initWIthNibName:@"yourNib"]; if (self) // do stuff return self; } – LightningStryk Mar 11 '14 at 14:40
  • I thought that works only for images, but this would be indeed better. – themarketka Mar 12 '14 at 15:21
3

The main reason that XIBs are separate files is because Apple feel that UIs designed for iPhones/iPod touches and iPads should be tailored to each respectively. This is echoed in their their iOS App Programming Guide, which says the following:

For views, the main modification is to redesign your view layouts to support the larger screen. Simply scaling existing views may work but often does not yield the best results. Your new interface should make use of the available space and take advantage of new interface elements where appropriate. Doing so is more likely to result in an interface that feels more natural to the user—and not just an iPhone app on a larger screen.

Whilst it can take time to maintain two XIBs for what is effectively one UI, I feel it is more straightforward than using one XIB and then having to connect up most of your UI elements in order to move them around programmatically when that XIB loads. After all, with two XIBs at least you can see what each UI looks like, and then make visual changes easily.

As an aside, don't forget iOS 5's Storyboards (read about them here), which make managing a view/view controller hierarchy much simpler.

  • I appreciate the information, and will take it under advisement. However, is there a way to develop only one .xib file? I personally feel the above quote from Apple is more relevant to applications than to games (and I am working with games). So while I appreciate and understand what you are saying, I would like to know if it is possible (and if so, how) to load to both iPhone and iPad from one .xib. I have no intention of changing my interface between the two devices in my games, so...it seems a waste of time to duplicate the work. – WendiKidd Apr 05 '12 at 16:14
  • Are you planning to draw directly into a view using Quartz or another drawing method? –  Apr 05 '12 at 17:15
  • Or maybe it's better we know exactly what your UI is going to do, and if it is going to be heavy with stock IB elements that will require connecting up. –  Apr 05 '12 at 17:37
  • 1
    It should not be, no. I am just trying to get the basic elements of the UI drawn out in the interface builder, after which I will continue to write the game logic in code. I suppose...my question is not whether or not this is advisable (though I appreciate the information on that standpoint) but whether or not it can be done, and if so, how. By default two separate xibs are created. I want one, and I want it to link to both iPhone and iPad. Can I do this, and how if so? Thanks. – WendiKidd Apr 05 '12 at 19:29
  • If I were to take a reasoned guess, one place to check is in the Project Summary, when your app target is selected: under the Deployment Info sections, there are Main Interface menus for iPhone and iPad, and you might be able to set both these to the same XIB. I have not tested this however. –  Apr 05 '12 at 20:54
  • I very much appreciate your continuing help :) But unfortunately this change causes the program to crash on startup, both on iPhone and iPad. – WendiKidd Apr 06 '12 at 12:39
1

Try to name them

MyCell.xib and MyCell ~ ipad.xib

then:

[self.tableView registerNib: @"MyCell" forCellReuseIdentifier: @"MyUniqueIdentifier"];
RAS
  • 8,100
  • 16
  • 64
  • 86
Pagly
  • 73
  • 7
0

If your using IB, you need to create 2 separate xib files for iPhone and iPad. You need a separate iPad xib to make your app comply with the Apple iPad UI guidelines.

kj13ennett
  • 319
  • 1
  • 2
  • 10
  • I know this is not true, because I have submitted non-interface-builder apps to the store in the past where the iPhone and iPad versions were identical. I probably should have noted in the question that I am making games, not applications. – WendiKidd Apr 05 '12 at 16:16