tabbar项目图像和selectedImage [英] tabbar item image and selectedImage

查看:91
本文介绍了tabbar项目图像和selectedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a tab bar controller (its a tab bar based application, so tab bar is on MainWindow.xib). In this xib, I have added 4 tab bar items and I have set the image of all tab bar item. Due to this, I am facing 2 issues:

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

1) The image is white-colored, but when I run the application, its showing all the images on tab bar item as gray colored. How can I make it look same as is in the original image.

2)我有一个选定的图像,我想在当前的标签栏项目上添加选择。我应该怎么做?

2) I have a selected image, that I want to add on the tab bar item which is currently selected. How should I do this???

在NICK的代码之后更新:

UPDATED AFTER NICK's CODE:

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

Hey, in iOS 5, you will have to write following code in your app delegate for setting tab bar item selected and unselected image (the category solution will work only on 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];
        }
    }
}


推荐答案

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

Add this category to your project. It will force tab bar items to use your original image as the disabled state instead of applying a grey gradient to them:

@implementation UItabBarItem (CustomUnselectedImage)

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

@end

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

This may seem like it is using private APIs but I've seen this used multiple times on apps that were approved. It's not actually calling a private method, just overriding one.

如果你需要为选定和未选择的图像指定不同的图像,那么你最好bet可能是使用UITabBarItem的tag属性和switch语句,如下所示:

If you need to specify different images for the selected and unselected image, your best bet is probably to use the tag property of the UITabBarItem and a switch statement, like this:

@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语句中指定的图像相匹配。

Then in interface builder, don't bother with setting the tab bar item images as they'll just be ignored. Instead, set their tags to match up with the images you've specified in your switch statements.

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

Note that if you have multiple tab bars in your app, and you don't want them to all be overridden in this way, you can define these methods on a subclass of UITabBarItem instead of as a category. Then you can set the class of the tab bar items in your nib file to be your custom subclass instead of regular UITabBarItems, and only those ones will be affected.

编辑:

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

Note that as of iOS 5 there is a better way of doing this using the UIAppearance APIs. This technique should still work, but who knows if Apple might start cracking down on it now that there is an officially supported approach. Better to use the new method unless you really need iOS 4 support.

这篇关于tabbar项目图像和selectedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆