开发者

Universal iPad/iPhone XIBs - Xcode 4

I want to make a universal app which has two different XIB files. One for iPhone, and one for iPad. They use the same code, just different UIs. How would开发者_C百科 I create a "universal" app?

Thanks.


Just duplicate the xib file, then rename it to FileName~ipad.xib, add it to your project, iOS will automatically load the correct xib file refer to your device.


As a start (you say you are creating a view based application), create it based upon either iPhone or an iPad view.

This will give you an appdelegate, a viewcontroller and a view (tailored for iPad or iPhone dependent upon which option you chose)

Now add another xib, go to File > New File... look on the left of the dialog and chooser "User Interface" in the iOS group. In the pane on the right, select View and click next, now choose iPad or iPhone (based on what you chose initially) when the xib is created, select it, then select the files owner on the left of the main pane. Then, go to Utilities (right pane) and choose Identity inspector (3rd option icon along the top) there change the class to be the same viewController as was created when you created your view based application. You can bind outlets just the same way on both views but they will share the same viewController.

Determining which device your app is running on at runtime, you can use the convention

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

and base the loading of views upon this kind of conditional statement. Just for clarity, remember you load a nib using its name so you can choose the nib relevant to the environment (above) and the framework will do the rest.

Be aware that it is never as straightforwards as you might think (if you've never done it before) Apps that make best use of the iPad's real estate generally tend to work better with dedicated views, although this is certainly not always the case. Any dynamically added screen components will need to be coded as such, taking into account the difference in screen space.

This could easily turn into an essay, I suggest you do some reading, check out source code and dive in. You'll learn a lot just by experimenting.


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 UIInterfaceOrientation des=self.interfaceOrientation;
 if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //ipad
 {
    if (des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)
    {
      //Ipad portarit
    } 
    else
    {
       //ipad landscape
    }
else //iPhone
{
if (des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)
    {
      //iPhone portarit
    } 
    else
    {
       //iPhone landscape
    }
}


Create a Macro like so:

#define IS_IPAD ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)

The name your XIBs so that you can do this:

self.viewController = [[ViewController alloc] initWithNibName:IS_IPAD?@"ViewController~iPad":@"ViewController" bundle:nil];


You also could use the abilities of xcode 4. There is an option while creating new projects which is called "universal app". Apps based upon this template use the mentioned separation. You get folders for iphone and ipad with views in it.


If you have existing app then there is option in Targets-->upgrade for iPad/iPhone

After that add code to check whether application is running in iPad or iPhone.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    //load xib of ipad
}
else
{
    //load xib of iphone
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜