iOS: Using device modifiers for loading xib files?

I had this same problem. The answer didn’t make sense at first, but the good news is that it’s easy to do! 🙂

Just name your iPad xibs without any modifier and your iPhone xibs with ~iphone modifier and it’ll select them correctly.

So, with MyViewController, you’ll have MyViewController.xib for the iPad and MyViewController~iphone.xib for the iPhone. Then you can just init your view controller with simple alloc/init.

[[MyViewController alloc] init] and it’ll grab the right xib.

So, when I create a new view controller in XCode, I always choose the box to format it for ipad, because the xib it will create will be named MyViewController.xib and you want that one to be the iPad sized xib. Then I create a second xib, formatted for iPhone and name it with the ~iphone modifier.

The documentation is a little contradictory at times, but this page talks about how resources with an identifier will default to iPad.

ImageSoundResources

Check the section about using high res images. I know we’re talking xibs and not images, but it does work. My last 6 apps have all used this idiom.

Leave a Comment