能够自定义图片(主要功能, 能够完成自己想要的效果)
下面是自定义了一个TabBar ,每个按钮可以显示图片(自定义避免了图片无法显示只显示一块颜色,在这里希望能够帮住到大家)
效果图:
下面的释放内存方式我采用了宏,参见前期博文
代码实现:
- (void)loadViewController
{
CoverCollectionViewController *coverVC = [[CoverCollectionViewController alloc] init];
coverVC.title = @"每日封面";
CustomizedNavigationController *coverNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:coverVC];
coverNaVC.navigationItem.title = @"iDailyWATCh";
NewsTableViewController *newsVC = [[NewsTableViewController alloc] init];
newsVC.title = @"腕表杂志";
CustomizedNavigationController *newsNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:newsVC];
BrandsTableViewController *brandsVC = [[BrandsTableViewController alloc] init];
brandsVC.title = @"品牌维基";
CustomizedNavigationController *brandsNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:brandsVC];
VideosCollectionViewController *videosVC = [[VideosCollectionViewController alloc] init];
videosVC.title = @"视频";
CustomizedNavigationController *videosNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:videosVC];
MoreViewController *moreVC = [[MoreViewController alloc] init];
moreVC.title = @"更多";
CustomizedNavigationController *moreNaVC = [[CustomizedNavigationController alloc] initWithRootViewController:moreVC];
self.viewControllers = @[coverNaVC, newsNaVC, brandsNaVC, videosNaVC, moreNaVC];
RELEASE_SAFE(coverNaVC);
RELEASE_SAFE(coverVC);
RELEASE_SAFE(brandsNaVC);
RELEASE_SAFE(brandsVC);
RELEASE_SAFE(videosNaVC);
RELEASE_SAFE(videosVC);
RELEASE_SAFE(moreNaVC);
RELEASE_SAFE(moreVC);
}
- (void)loadCustomizedTabBarView
{
//初始化自定义TabBar背景
UIImageView *tabBarBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 519, 320, 49)];
tabBarBG.userInteractionEnabled = YES;
tabBarBG.image = [UIImage imageNamed:@"background"];
[self.view addSubview:tabBarBG];
[tabBarBG release];
//初始化自定义TabBarItem -> Button
for (int index = 0; index < 5; index++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor whiteColor];
button.tag = index;
NSString *imageName = [NSString stringWithFormat:@"%d", index + 100];
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
button.frame = CGRectMake(3 + 64 * index, 5, 290 / 5, 39);
[tabBarBG addSubview:button];
[button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
}
}
//实现对应按钮和页面的跳转
- (void)changeViewController:(UIButton *)button
{
self.selectedIndex = button.tag;
}