UITabBar tint in iOS 7

后端 未结 4 2445
春和景丽
春和景丽 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条回答
  •  萌比男神i
    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.

提交回复
热议问题