UITabBar tint in iOS 7

后端 未结 4 2458
春和景丽
春和景丽 2021-02-19 15:31

How can i specify the tint of images when a tab is selected and unselected?

I have tried this but it doesnt work:

[[UITabBar appearance] setTintColor:[UI         


        
相关标签:
4条回答
  • 2021-02-19 15:39

    You have to use the new Image rendering modes introduced in iOS 7 (UIImageRenderingModeAlwaysOriginal and UIImageRenderingModeAlwaysTemplate )see my answer to a similar question:

    Hope this helps

    0 讨论(0)
  • 2021-02-19 15:40

    Are you using the template-version of your images?

    Instead of setting your images with [UIImage imageNamed: @"MyImage"], set them with [[UIImage imageNamed: @"MyImage"] imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate].

    This setup along with your code should solve this issue.

    0 讨论(0)
  • 2021-02-19 16:00

    You can set the tint color for selected and unselected tab bar buttons like this:

    [[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
    

    The first line sets the unselected color - red in this example - by setting the UIView's tintColor when it's contained in a tab bar. Note that this only sets the unselected image's tint color - it doesn't change the color of the text below it.

    The second line sets the tab bar's selected image tint color to green.

    0 讨论(0)
  • 2021-02-19 16:00

    if you do not have many viewcontrollers. Here is my way to do it.

    In your delegate method just place your tabbar bg Image. And set the UIImageView

    Create UITabbar intance in AppDelegate.h

    @property (nonatomic,retain) UITabBar *tabbar;
    

    And

    @synthesize tabbar;
    
    UITabBarController *tabBarController =
        (UITabBarController *)self.window.rootViewController;
    tabbar = [tabBarController tabBar];
    
    [tabbar setBackgroundImage:[UIImage imageNamed:@"tabbarBg.png"]];
     NSArray *tabImageArray  = [NSArray arrayWithObjects:
                                    [UIImage imageNamed:@"tab1Hover.png"],
                                    [UIImage imageNamed:@"tab2.png"],
                                    [UIImage imageNamed:@"tab3.png"],
                                    [UIImage imageNamed:@"tab4.png"],
                                    [UIImage imageNamed:@"tab5.png"],
                                   nil];
    
        for (int i = 0; i<5; i++) {
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20+i*60+i*3.5, 10, 25, 21)];
            [image setContentMode:UIViewContentModeScaleAspectFit];
            [image setImage:[tabImageArray objectAtIndex:i]];
            [image setTag:10+i];
            [tabbar addSubview:image];
        } 
    

    Then every ViewController in tabbar add

    -(void)viewWillAppear:(BOOL)animated 
    

    delegate method and in this method. You can change the Imageviews as shown below.

    -(void)viewWillAppear:(BOOL)animated{
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    UITabBarController *tabBarController = (UITabBarController *)appDelegate.window.rootViewController;
     NSArray *tabImageArray  = [NSArray arrayWithObjects:
                                   [UIImage imageNamed:@"tab1Hover.png"],
                                   [UIImage imageNamed:@"tab2.png"],
                                   [UIImage imageNamed:@"tab3.png"],
                                   [UIImage imageNamed:@"tab4.png"],
                                   [UIImage imageNamed:@"tab5.png"],
                                   nil];
    for (int i = 0; i<5; i++) {
            UIImageView *image = (UIImageView*)[tabbar viewWithTag:10+i];
            [image setImage:[tabImageArray objectAtIndex:i]];
        }
    }
    

    So, just costumize tabImageArray in every View controller. Then you can use it.

    I works on iOS 7 as well.

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