//首先,要知道,往往想法是好的开始最关键的一步。对于这个小功能也是如此。当初我的想法就是,两张图片分别赋给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