2012-07-26 163 views
0

我正在創建選項卡式應用程序,並且在選項卡上顯示圖標時遇到了一些麻煩。我將我的代碼與我在本書中研究過的前一個示例進行了比較,它似乎匹配,但新應用程序中的圖標僅顯示爲空白方塊。我試圖將所有相關的代碼都包含進來,但是如果您希望看到更多內容,只需詢問一下,然後根據需要編輯更多內容。我已經檢查過imageNamed:string和圖標名稱的情況是相同的。XCode選項卡式應用程序不會加載圖標

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    ... // initializers for ViewControllers 1-3, which are custom viewControllers 
     // that have the tab properties set in (void)viewDidLoad 
    globalUINavigationController = [[UINavigationController alloc] initWithNibName: 
           @"DemoTabbedFourthViewController" bundle:nil]; 
    globalUINavigationController.title = NSLocalizedString(@"Fourth", @"Fourth"); 
    globalUINavigationController.tabBarItem.image = [UIImage imageNamed:@"fourth.png"]; 

    UIViewController *viewController4 = [[DemoTabbedFourthViewController alloc] 
        initWithNibName:@"DemoTabbedFourthViewController" bundle:nil]; 

    [globalUINavigationController pushViewController:viewController4 animated:YES]; 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, 
             viewController3, globalUINavigationController, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
} 

我使用的globalUINavigationController作爲我的榜樣,因爲如果我能得到一個工作,我應該能夠得到別人的工作基礎上。如果您對代碼有任何問題或意見,請告訴我。我是Objective C和iPhone App開發的新手,我會盡我所能的幫助。

回答

0

我認爲這個問題是你初始化的UINavigationController用筆尖文件。我其實從來沒有嘗試過。您應該首先初始化ViewController,然後將其添加到NavigationController的init消息中。此外,您必須在內容視圖控制器上設置標題,而不是在導航控制器上設置標題,因爲導航控制器要求其topViewController顯示標題。

我還沒有編譯下面的代碼,所以可能會有一些丟失的逗號或類似的東西,但它應該讓你知道我的意思。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    ... // initializers for ViewControllers 1-3, which are custom viewControllers 
     // that have the tab properties set in (void)viewDidLoad 
    UIViewController *viewController4 = [[DemoTabbedFourthViewController alloc] 
       initWithNibName:@"DemoTabbedFourthViewController" bundle:nil]; 

    globalUINavigationController = [[UINavigationController alloc] initWithRootViewController:viewController4]; 
    globalUINavigationController.tabBarItem.image = [UIImage imageNamed:@"fourth.png"]; 

    [globalUINavigationController pushViewController:viewController4 animated:YES]; 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, 
            viewController3, globalUINavigationController, nil]; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
} 

如果這不能解決問題,可能是因爲您的圖標文件是常規圖像。只有圖像的Alpha通道用於標籤欄項目。

//編輯:在這種情況下,看看這個問題:UITabBarItem images just appear as a grey block

+0

你是對的,我需要讓他們成爲不同的圖像類型。該文章有兩個樣本圖像適合我。謝謝你的幫助! – Chance 2012-07-26 16:33:22

0

這是因爲圖像太大或圖標是正方形。我的意思是,XCode只會將您的圖像作爲蒙版(意味着顏色無關緊要)。它必須是PNG圖像,大小約30x30。從課堂參考;

圖片 該商品的圖片。如果爲零,則不顯示圖像。 選項卡欄上顯示的圖像來源於此圖像。如果此圖片太大而無法放在標籤欄上,則會剪切以適合該圖片。標籤欄圖像的大小通常爲30 x 30點。源圖像中的alpha值用於創建未選中和選定的圖像 - 不透明值將被忽略。

+0

他們是PNG圖像,並且圖像是30×30(60×60的視網膜顯示圖像,但這些都是適當命名,即「第四@ 2x.png「)。我不太明白「方形圖標」的意思,你能否再解釋一下? – Chance 2012-07-26 16:17:51

+0

圖標是否具有透明度? [Here](http://devinsheaven.com/creating-uitabbar-uitoolbar-icons-in-adobe-illustrator/)是Illustrator中的一個示例。 – ohr 2012-07-26 16:21:37

相關問題