开发者

How to make iPhone and iPad version of an app?

开发者 https://www.devze.com 2023-02-08 23:37 出处:网络
I am trying to make an app which works on both iPhone and iPad. I am looking how to make an interface compatible on both. When the app loads I am displaying a table view. How can I load different nibs

I am trying to make an app which works on both iPhone and iPad. I am looking how to make an interface compatible on both. When the app loads I am displaying a table view. How can I load different nibs based on device? I am using this to switch between nibs.

if ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)]) 
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
    开发者_如何学编程    {
            device = @"iPad";
        }
        else 
        {
            device = @"iPhone";
        }

    }

But in MainWindow.xib it says view is loaded from the view controller for iPhone. Can I make this dynamic based on device? ie I want to show from the start of app different nibs based on device. Thanks.


Actually, Apple does all this automatically, just name your NIB files:

MyViewController~iphone.xib // iPhone
MyViewController~ipad.xib // iPad

and load your view controller with the smallest amount of code:

[[MyViewController alloc] initWithNibName:nil bundle:nil]; // Apple will take care of everything


You can do that in a similar way:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
    mainMenu = [[MainMenu alloc] initWithNibName:@"MainMenuiPad" bundle:nil]; 
}else{  
    mainMenu = [[MainMenu alloc] initWithNibName:@"MainMenu" bundle:nil]; 
}


For making universal app,

1-set target family in info build tab for app as iPhone/iPad.

2- Delete window from main window.

3- Add two xib one for iPhone and one for iPad(by selecting iPad xib).

4- make appDelegate class as controller file for these xib's.

5- Add window on these xibs and view controller or navigation controller and by IB Inspector set load nib name and controller file here which one is your first view.

6- And then make differnet xib for iPad and iPhone which having tableview or other controls.

7-Make single contoller file or different controler file for different device for same you need to check the device by this if else condition

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

8-Now you need to load xib in appDelegate class in method didFinishL--

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

   // load your nib for iPad here which having view controler or navigation controller as well window.
}else{  
    //load nib for iPhone. 
}


Try this in your Info.plist:
Main nib file base name [NSMainNibFile]: MainWindow_iPhone
Main nib file base name (iPad) [NSMainNibFile~ipad]: MainWindow_iPad

I hope this helps you.

0

精彩评论

暂无评论...
验证码 换一张
取 消