如何实现点击tabBar,让item改变图片,或者说是改变颜色

//首先,要知道,往往想法是好的开始最关键的一步。对于这个小功能也是如此。当初我的想法就是,两张图片分别赋给UIButton,然后点击的时候,显示高亮的那张图片,可是最后做的都是无用功。

//这个小功能里,我的想法就是在自定义的tabBar视图上放五个UIButton,然后分别在这五个Button上面放五张有normal和highlight的图片上去,也就是十张图片。然后在按钮的点击事件中,让被选中的button显示高亮,其他的显示normal。 
废话不多说,直接上代码

#import "MainViewController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "DiscoverViewController.h"
#import "ProfileViewController.h"
#import "MoreViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBar.hidden = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initViews];
    [self initTabBar];

}

- (void)initViews
{
    HomeViewController *home = [[HomeViewController alloc] init];
    MessageViewController *message = [[MessageViewController alloc] init];
    DiscoverViewController *discover = [[DiscoverViewController alloc] init];
    ProfileViewController *profile = [[ProfileViewController alloc] init];
    MoreViewController *more = [[MoreViewController alloc] init];
    
    NSArray *array = @[home,message,discover,profile,more];
    
    NSMutableArray *viewContrllerArray = [NSMutableArray arrayWithCapacity:5];
    for (int index = 0; index<array.count; index ++) {
        UINavigationController *baseNav = [[UINavigationController alloc] initWithRootViewController:array[index]];
        [viewContrllerArray addObject:baseNav];
        [baseNav release];
    }
    self.viewControllers = viewContrllerArray;
}

- (void)initTabBar
{
    _tabBarImage = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].applicationFrame.size.height-49+20, 320, 40)];
    _tabBarImage.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background@2x.png"]];
    [self.view addSubview:_tabBarImage];
    
    NSArray *backArray = @[@"tabbar_home@2x.png",@"tabbar_message_center@2x.png",@"tabbar_discover@2x.png",@"tabbar_profile@2x.png",@"tabbar_more@2x.png"];
    _highlightArray = @[@"tabbar_home_highlighted@2x.png",@"tabbar_message_center_highlighted@2x.png",@"tabbar_discover_highlighted@2x.png",@"tabbar_profile_selected@2x.png",@"tabbar_more_highlighted@2x.png"];
    
    for (int i = 0; i<backArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:backArray[i]] highlightedImage:[UIImage imageNamed:_highlightArray[i]]];
        imageView.frame = CGRectMake(0, 0, 30, 30);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((64-30)/2+(64*i), (49-30)/2, 30, 30);
        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        [button addSubview:imageView];
        [imageView release];
        [_tabBarImage addSubview:button];
    }
    
    // 初始化时,将其设置为高亮
    UIButton *button = _tabBarImage.subviews[0];
    UIImageView *imageView = button.subviews[0];
    imageView.highlighted = YES;
}

// 点击图标的事件方法
- (void)clickAction:(UIButton *)button
{
    self.selectedIndex = button.tag;
    
    NSArray *array = _tabBarImage.subviews;
    for (UIButton *bt in array) {
        if (bt.tag == button.tag) {
            UIImageView *imageView = bt.subviews[0];
            imageView.highlighted = YES;
        } else {
            UIImageView *imageView = bt.subviews[0];
            imageView.highlighted = NO;
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

@end


转载于:https://my.oschina.net/u/1982653/blog/289307

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值