tabbar item image and selectedImage

前端 未结 2 1938
误落风尘
误落风尘 2020-12-06 04:10

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

相关标签:
2条回答
  • 2020-12-06 04:21

    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
    

    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.

    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
    

    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.

    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.

    EDIT:

    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.

    0 讨论(0)
  • 2020-12-06 04:28

    Based on http://blog.theanalogguy.be/ works for me. Add the category UItabBarItem (CustomUnselectedImage) - haven't effect =(

    the *.h

    @interface CustomTabBarItem : UITabBarItem {  
        UIImage *customHighlightedImage;  
        UIImage *customNormalImage;  
    }  
    
    @property (nonatomic, retain) UIImage *customHighlightedImage;  
    @property (nonatomic, retain) UIImage *customNormalImage;  
    
    - (id)initWithTitle:(NSString *)title 
            normalImage:(UIImage *)normalImage 
        highlightedImage:(UIImage *)highlightedImage 
                    tag:(NSInteger)tag;
    @end
    

    and *.m

    #import "CustomTabBarItem.h"
    
    @implementation CustomTabBarItem  
    
    @synthesize customHighlightedImage;  
    @synthesize customNormalImage;  
    
    - (id)initWithTitle:(NSString *)title 
            normalImage:(UIImage *)normalImage 
        highlightedImage:(UIImage *)highlightedImage 
                    tag:(NSInteger)tag{
    
        [self initWithTitle:title
                        image:nil
                        tag:tag];
        [self setCustomNormalImage:normalImage];
        [self setCustomHighlightedImage:highlightedImage];
        return self;
    }
    
    - (void) dealloc  
    {  
        [customHighlightedImage release];
        customHighlightedImage=nil;  
        [customNormalImage release]; 
        customNormalImage=nil;  
        [super dealloc];  
    }  
    
    -(UIImage *) selectedImage  
    {  
        return self.customHighlightedImage;  
    }  
    
    -(UIImage *) unselectedImage  
    {  
        return self.customNormalImage;  
    }  
    
    @end  
    

    happy coding =]-

    0 讨论(0)
提交回复
热议问题