简体   繁体   中英

ios Tabbar selected index set image on didSelectViewController

I am Creating One Tab-bar by programmatically , Tab-bar working Properly the Problem is I don't know How to set selected index , selected tab-bar set blue color , set blue image Like How to know this tab-bar item is selected , didSelectViewController delegate method is used but i don't understand how set image This is method using but i dont know how to set image and color

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Here is my code

-(IBAction)clicka:(id)sender
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    AViewController *viewController1 = [[AViewController alloc] initWithNibName:@"AViewController" bundle:nil];





    BViewController *viewController2 = [[BViewController alloc] initWithNibName:@"BViewController" bundle:nil];



    CViewController *viewController3 = [[CViewController alloc]initWithNibName:@"CViewController" bundle:nil];



    DViewController *viewController4 = [[DViewController alloc]initWithNibName:@"DViewController" bundle:nil];



    [self.navigationController pushViewController:viewController1 animated:YES];



    self.tabBarController = [[UITabBarController alloc] init];



    self.tabBarController.delegate=self;

    self.tabBarController.viewControllers = @[viewController1,viewController2,viewController3,viewController4];



    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:self.tabBarController];

    UIImageView   *img = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,100)];

    img.image=[UIImage imageNamed:@"yellow-bg.png"];

    [self.tabBarController.tabBar addSubview:img];





    UIImageView *imghome=[[UIImageView alloc]initWithFrame:CGRectMake(30.0,5,25,25)];

    imghome.image=[UIImage imageNamed:@"splash-logo.png"];

    [img addSubview:imghome];



    UIImageView *imghome1=[[UIImageView alloc]initWithFrame:CGRectMake(100.0,5,25,25)];

    imghome1.image=[UIImage imageNamed:@"chat-icon.png"];

    [img addSubview:imghome1];



    UIImageView *imghome2=[[UIImageView alloc]initWithFrame:CGRectMake(180.0,5,25,25)];

    imghome2.image=[UIImage imageNamed:@"p-icon.png"];

    [img addSubview:imghome2];

    UIImageView *imghome3=[[UIImageView alloc]initWithFrame:CGRectMake(260.0,5,25,25)];

    imghome3.image=[UIImage imageNamed:@"addddd.png"];

    [img addSubview:imghome3];

    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];


}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSUInteger selectedIndex = self.tabBarController.selectedIndex;
NSLog(@"%lu",(unsigned long)selectedIndex);
}

please Help me

You can find out the render mode in UIImage.h

- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode

By the default , if the image was attached to a tabbar as tabbarItem, the rednering mode is UIImageRenderingModeAlwaysTemplate, so it's is blue.

You should change the image rendering mode before you set to the tabbaritem.

UIImage * originalImage = [UIImage imageNamed:@"chat-icon.png"];
UIImage * selectedBlueImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

when the tab was selected, set the selectedBlueImage to the imageView. like : imageview.highlightedImage = selectedBlueImage

I am not sure if I understood you problem correctly, but if you want to set images inside each TabBar section for each View Controllers correspondingly, you can do that from each View Controller class, for example, inside AViewController.m, override:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        //self.tabBarItem.title = @"Title";
        self.tabBarItem.image = [UIImage imageNamed:@"image-name"];
    }

    return self;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM