【问题标题】:XCode Tabbed Application won't load iconsXCode 选项卡式应用程序不会加载图标
【发布时间】:2012-07-26 16:05:34
【问题描述】:

我正在创建一个选项卡式应用程序,但在让我的图标显示在选项卡上时遇到了一些问题。我将我的代码与我之前在书中编写的示例进行了比较,它似乎匹配,但新应用程序中的图标只是显示为空白方块。我已尝试包含所有相关代码,但不包含所有繁琐的部分,但如果您想查看更多内容,请询问,我会根据需要进行更多编辑。我已经检查过 imageNamed:string 和图标名称的大小写是否相同。

- (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 作为我的示例,因为如果我可以让那个工作,我应该能够让其他人基于它工作。如果您对代码有任何疑问或 cmets,请告诉我。我是 Objective C 和 iPhone 应用程序开发的新手,我会尽我所能获得帮助。

【问题讨论】:

    标签: objective-c ios cocoa-touch


    【解决方案1】:

    我认为问题在于您使用 nib 文件初始化 UINavigationController。我实际上从未尝试过这个。您应该首先初始化 ViewController,然后将其添加到 NavigationController 的初始消息中。此外,您必须在内容视图控制器上设置标题,而不是在导航控制器上,因为导航控制器会向其 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

    【讨论】:

    • 你是对的,我需要让它们成为不同的图像类型。这篇文章有两个对我有用的示例图像。感谢您的帮助!
    【解决方案2】:

    那是因为图像太大,或者您的图标是正方形。我的意思是,XCode 只会将您的图像作为蒙版抓取(意味着颜色无关紧要)。它必须是 PNG 图像,大小约为 30x30。来自类参考;

    图片 商品的图片。如果为零,则不显示图像。 标签栏上显示的图像来自此图像。如果此图像太大而无法放在选项卡栏上,则会对其进行裁剪以适应。标签栏图像的大小通常为 30 x 30 点。源图像中的 alpha 值用于创建未选中和选中的图像——不透明的值将被忽略。

    【讨论】:

    • 它们是 PNG 图像,图像大小为 30x30(Retina Display 图像为 60x60,但命名恰当,即“fourth@2x.png”)。我不太明白你所说的“方形图标”是什么意思,你能再解释一下吗?
    • 图标有透明度吗? Here 是 Illustrator 中的一个示例。
    猜你喜欢
    • 2011-12-18
    • 2012-04-13
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多