自定义TabBar第一种方案

  79人阅读   评论(0)   收藏   举报

自定义tabBar能够解决自定义图片无法显示(只显示一块颜色)这个弊端,自定义tabBar要继承UITabBarController

原图片:

(1),  (2), (3),  (4), (5)

下面是自定义了一个TabBar ,每个按钮可以显示图片(自定义避免了图片无法显示只显示一块颜色)

效果图:




代码实现:

- (void)loadViewController

{

    CoverCollectionViewController *coverVC = [[CoverCollectionViewController allocinit];

    coverVC.title = @"每日封面";

    CustomizedNavigationController *coverNaVC = [[CustomizedNavigationController alloc]initWithRootViewController:coverVC];

    coverNaVC.navigationItem.title = @"iDailyWATCh";

    

    NewsTableViewController *newsVC = [[NewsTableViewController allocinit];

    newsVC.title = @"腕表杂志";

    CustomizedNavigationController *newsNaVC = [[CustomizedNavigationController alloc]initWithRootViewController:newsVC];

    

    BrandsTableViewController *brandsVC = [[BrandsTableViewController allocinit];

    brandsVC.title = @"品牌维基";

    CustomizedNavigationController *brandsNaVC = [[CustomizedNavigationController alloc]initWithRootViewController:brandsVC];

    

    VideosCollectionViewController *videosVC = [[VideosCollectionViewController allocinit];

    videosVC.title = @"视频";

    CustomizedNavigationController *videosNaVC = [[CustomizedNavigationController alloc]initWithRootViewController:videosVC];

    

    MoreViewController *moreVC = [[MoreViewController allocinit];

    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 allocinitWithFrame:CGRectMake(051932049)];

    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, 5290 / 539);

        [tabBarBG addSubview:button];

        [button addTarget:self action:@selector(changeViewController:)forControlEvents:UIControlEventTouchUpInside];

    }

}

//实现对应按钮和页面的跳转

- (void)changeViewController:(UIButton *)button

{

    self.selectedIndex = button.tag;

}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值