iphone - 标签栏项目图像和 selectedImage

标签 iphone ios uitabbarcontroller uitabbaritem

我有一个标签栏 Controller (它是一个基于标签栏的应用程序,所以标签栏在 MainWindow.xib 上)。在这个 xib 中,我添加了 4 个标签栏项目,并设置了所有标签栏项目的图像。因此,我面临两个问题:

1)图像是白色的,但是当我运行应用程序时,它会将选项卡栏项目上的所有图像显示为灰色。如何使它看起来与原始图像中的相同。

2) 我有一个选定的图像,我想将其添加到当前选定的选项卡栏项目上。我该怎么做???

尼克的代码后更新:

嘿,在 iOS 5 中,您必须在应用程序委托(delegate)中编写以下代码来设置选项卡栏项目选中和未选中的图像(类别解决方案仅适用于 4):

if ([[[UIDevice currentDevice] systemVersion] floatValue]>4.9) {
    NSString *selectedImageName,*unselectedImageName;

    for (int counter = 0; counter < [self.tabBarController.tabBar.items count]; counter++) {
        if (counter==0) {
            selectedImageName = <someImagename>;
            unselectedImageName = <someImagename>;
        }
        else if (counter==1) {
            selectedImageName = <someImagename>;
            unselectedImageName = <someImagename>;
        }
        .
                    .
        else {
            selectedImageName = <someImagename>;
            unselectedImageName = <someImagename>;
        }
        UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
        UIImage *unselectedImage = [UIImage imageNamed:unselectedImageName];

        UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:counter];
        if ([item respondsToSelector:@selector(setFinishedSelectedImage:withFinishedUnselectedImage:)]) {
            [item setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
        }
    }
}

最佳答案

将此类别添加到您的项目中。它将强制标签栏项目使用您的原始图像作为禁用状态,而不是对其应用灰色渐变:

@implementation UItabBarItem (CustomUnselectedImage)

- (UIImage *)unselectedImage
{
    return self.image;
}

@end

这可能看起来像是在使用私有(private) API,但我已经看到它在被批准的应用程序上多次使用。它实际上并没有调用私有(private)方法,只是覆盖了一个。

如果您需要为选中和未选中的图片指定不同的图片,最好的办法可能是使用 UITabBarItem 的 tag 属性和 switch 语句,如下所示:
@implementation UItabBarItem (Custom)

- (UIImage *)selectedImage
{
    switch (self.tag)
    {
        case 1:
            return [UIImage imageNamed:@"tab-selected1.png"];
        case 2:
            return [UIImage imageNamed:@"tab-selected2.png"];
        etc...
    }
}

- (UIImage *)unselectedImage
{
    switch (self.tag)
    {
        case 1:
            return [UIImage imageNamed:@"tab-unselected1.png"];
        case 2:
            return [UIImage imageNamed:@"tab-unselected2.png"];
        etc...
    }
}

@end

然后在界面生成器中,不要费心设置标签栏项目图像,因为它们会被忽略。相反,将它们的标签设置为与您在 switch 语句中指定的图像相匹配。

请注意,如果您的应用程序中有多个选项卡栏,并且您不希望它们都以这种方式被覆盖,您可以在 UITabBarItem 的子类上定义这些方法,而不是作为类别。然后,您可以将 nib 文件中的选项卡栏项的类设置为您的自定义子类,而不是常规的 UITabBarItems,并且只有那些会受到影响。

编辑:

请注意,从 iOS 5 开始,使用 UIAppearance API 可以更好地执行此操作。这种技术应该仍然有效,但谁知道苹果现在是否会开始打击它,因为有官方支持的方法。除非您真的需要 iOS 4 支持,否则最好使用新方法。

关于iphone - 标签栏项目图像和 selectedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8939399/

相关文章:

ios - swift : How to add icon image and image background to the UItabBar in App delegate?

ios - 在 iPhoneX 上设置 UITabBarController 的 ViewControllers 会错放标签

iphone - 支持方向 IOS

iphone - 一个强大的、功能齐全的 UIWebView Controller 实现?

ios - 允许客户在不给他们代码的情况下测试应用程序

c++ - 使用 OpenCV 的体系结构 x86_64 的 undefined symbol

objective-c - 跨 subview Controller 从 UITabBarController 继承/保留 UINavigationBar?

iphone - 当设备旋转时,如何显示横向 coverflow 样式 View ?

ios - 创建选项变量时出现 Swift 代码错误

iOS 属性字符串高度