Universal iPad/iPhone XIBs - Xcode 4

给你一囗甜甜゛ 提交于 2019-12-20 15:31:37

问题


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 I create a "universal" app?

Thanks.


回答1:


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.




回答2:


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.




回答3:


- (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
    }
}



回答4:


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];



回答5:


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.




回答6:


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
}


来源:https://stackoverflow.com/questions/6271442/universal-ipad-iphone-xibs-xcode-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!