簡體   English   中英

通用 iPad/iPhone XIB - Xcode 4

[英]Universal iPad/iPhone XIBs - Xcode 4

我想制作一個具有兩個不同 XIB 文件的通用應用程序。 一個用於 iPhone,一個用於 iPad。 他們使用相同的代碼,只是不同的用戶界面。 我將如何創建一個“通用”應用程序?

謝謝。

只需復制 xib 文件,然后將其重命名為FileName~ipad.xib ,將其添加到您的項目中,iOS 將根據您的設備自動加載正確的 xib 文件。

首先(您說您正在創建基於視圖的應用程序),基於 iPhone 或 iPad 視圖創建它。

這將為您提供一個 appdelegate、一個視圖控制器和一個視圖(根據您選擇的選項為 iPad 或 iPhone 量身定制)

現在添加另一個 xib,go 到 File > New File... 查看對話框左側並選擇 iOS 組中的“用戶界面”。 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. 然后,go 到實用程序(右窗格)並選擇身份檢查器(頂部的第三個選項圖標)將 class 更改為與創建基於視圖的應用程序時創建的相同的 viewController。 您可以在兩個視圖上以相同的方式綁定 outlet,但它們將共享相同的 viewController。

確定您的應用在運行時運行在哪個設備上,您可以使用約定

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

並基於這種條件語句加載視圖。 為清楚起見,請記住您使用其名稱加載 nib,以便您可以選擇與環境相關的 nib(上圖),框架將執行 rest。

請注意,它從來沒有您想象的那么簡單(如果您以前從未這樣做過) 充分利用 iPad 的房地產的應用程序通常傾向於使用專用視圖更好地工作,盡管情況肯定並非總是如此。 考慮到屏幕空間的差異,任何動態添加的屏幕組件都需要這樣編碼。

這很容易變成一篇文章,我建議你做一些閱讀,查看源代碼並深入研究。通過實驗你會學到很多東西。

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

像這樣創建一個宏:

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

為您的 XIB 命名,以便您可以執行此操作:

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

如果您有現有的應用程序,那么 Targets-->upgrade for iPad/iPhone 中有選項

之后添加代碼以檢查應用程序是否在 iPad 或 iPhone 中運行。

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

您還可以使用 xcode 4 的功能。創建新項目時有一個選項,稱為“通用應用程序”。 基於此模板的應用程序使用上述分隔。 您將獲得 iphone 和 ipad 的文件夾,其中包含視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM