2012-07-26 64 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

相关问题