自定义tabBar能够解决自定义图片无法显示(只显示一块颜色)这个弊端,自定义tabBar要继承UITabBarController
原图片:
(1), (2),
(3),
(4),
(5)
下面是自定义了一个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];
//使用的宏释放内存:(#define RELEASE_SAFE(_Pointer) do{[_Pointer release],_Pointer = nil;}while(0)
)
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.p_w_picpath = [UIImage p_w_picpathNamed:@"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 *p_w_picpathName = [NSString stringWithFormat:@"%d", index + 100];
[button setBackgroundImage:[UIImage p_w_picpathNamed:p_w_picpathName] 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;
}
转载于:https://blog.51cto.com/9178463/1562524