ios Tabbar在didSelectViewController上选择了索引集图像

时间:2014-08-15 05:36:33

标签: ios tabbar

我正在通过编程创建一个标签栏,标签栏工作正常问题是我不知道如何设置所选索引,选中标签栏设置蓝色,设置蓝色图像 喜欢如何知道这个标签栏项目被选中,didSelectViewController委托方法被使用,但我不明白如何设置图像 这是方法使用,但我不知道如何设置图像和颜色

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

这是我的代码

-(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);
}

请帮帮我

2 个答案:

答案 0 :(得分:0)

您可以在UIImage.h中找到渲染模式

- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode

默认情况下,如果图像作为tabbarItem附加到tabbar,则rednering模式为UIImageRenderingModeAlwaysTemplate,因此它为蓝色。

在设置为tabbaritem之前,您应该更改图像渲染模式。

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

选择选项卡后,将selectedBlueImage设置为imageView。像:imageview.highlightedImage = selectedBlueImage

答案 1 :(得分:0)

我不确定我是否正确理解了您的问题,但如果您想在每个ViewBinder部分内为每个View控制器设置相应的图像,您可以从每个View Controller类执行此操作,例如,在AViewController.m中,覆盖:

- (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;
}