iOS开发-自定义TabBarController添加跳转控制器并且进行封装(根据自定义TabBar)

本文详细介绍了如何在iOS应用中自定义TabBar,包括初始化TabBar、子控制器的配置以及TabBar事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  ZZTabBarController.h

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/9.

//  Copyright (c) 2015年 ZZ_Macpro. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface ZZTabBarController :UITabBarController

 

@end

 

 

//

//  ZZTabBarController.m

//  ZZ_APP主流框架

//

//  Created by ZZ_Macpro on 15/10/9.

//  Copyright (c) 2015年 ZZ_Macpro. All rights reserved.

//

 

#import "ZZTabBarController.h"

#import "ZZHomeController.h"

#import "ZZMeController.h"

#import "ZZDiscoverController.h"

#import "ZZMessageController.h"

#import "ZZComposeController.h"

#import "ZZTabBar.h"

#import "ZZNavigationController.h"

 

@interface ZZTabBarController () <ZZTabBarDelegate>

@property (nonatomic,weak)ZZTabBar *customTabBar;

@end

 

@implementation ZZTabBarController

 

- (void)viewDidLoad {

    [superviewDidLoad];

    

    // 1.初始化tabbar

    [selfsetupTabBar];

    

    // 2.初始化子控制器

    [selfsetupChildViewControllers];

}

 

/**

 *  每次即将显示的时候必须移除系统自带的

 */

- (void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

    

    // 移除之前的4个UITabBarButton

   for (UIView *childinself.tabBar.subviews) {

       if ([childisKindOfClass:[UIControlclass]]) {//UITabBarButton

            [childremoveFromSuperview];

        }

    }

}

 

/**

 *  初始化tabbar

 */

- (void)setupTabBar

{

   ZZTabBar *customTabBar = [[ZZTabBaralloc]init];

    // 自定义的tabbar 覆盖系统的就行

    customTabBar.frame =self.tabBar.bounds;

    customTabBar.delegate =self;

    [self.tabBaraddSubview:customTabBar];

   self.customTabBar = customTabBar;

}

 

/**

 *  初始化子控制器

 */

- (void)setupChildViewControllers

{

    // 1.首页

    ZZHomeController *home = [[ZZHomeControlleralloc]init];

    home.tabBarItem.badgeValue =@"4";

    [selfsetupChildViewController:hometitle:@"首页"imageName:@"tabbar_home"selectedImageName:@"tabbar_home_selected"];

    

    // 2.消息

    ZZMessageController *message = [[ZZMessageControlleralloc]init];

    message.tabBarItem.badgeValue =@"40";

    [selfsetupChildViewController:messagetitle:@"消息"imageName:@"tabbar_message_center"selectedImageName:@"tabbar_message_center_selected"];

    

    // 3.广场

    ZZDiscoverController *discover = [[ZZDiscoverControlleralloc]init];

    discover.tabBarItem.badgeValue =@"10";

    [selfsetupChildViewController:discovertitle:@"广场"imageName:@"tabbar_discover"selectedImageName:@"tabbar_discover_selected"];

    

    // 4.我

    ZZMeController *me = [[ZZMeControlleralloc]init];

    me.tabBarItem.badgeValue =@"5";

    [selfsetupChildViewController:metitle:@""imageName:@"tabbar_profile"selectedImageName:@"tabbar_profile_selected"];

}

 

/**

 *  添加一个子控制器

 *

 *  @param vc                子控制器

 *  @param title             标题

 *  @param imageName         图片

 *  @param selectedImageName 选中的图片

 */

- (void)setupChildViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName

{

    // 1.设置数据

    vc.title = title;

    vc.tabBarItem.image = [UIImageimageWithName:imageName];

    vc.tabBarItem.selectedImage = [UIImageoringinalImageWithName:selectedImageName];

    

    // 2.包装

    ZZNavigationController *nav = [[ZZNavigationControlleralloc]initWithRootViewController:vc];

    [selfaddChildViewController:nav];

    

    // 3.添加按钮

    [self.customTabBaraddTabBarButtonWithItem:vc.tabBarItem];

}

 

/**

 *  tabBar选中了按钮

 *

 *  @param from   以前的位置

 *  @param to     现在的位置

 */

- (void)tabBar:(ZZTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to

{

    self.selectedIndex = to;

}

 

/**

 *  选中+号按钮

 */

- (void)tabBarDidClickedPlusButton:(ZZTabBar *)tabBar

{

    ZZComposeController *compose = [[ZZComposeControlleralloc]init];

    ZZNavigationController *nav = [[ZZNavigationControlleralloc]initWithRootViewController:compose];

    [selfpresentViewController:navanimated:YEScompletion:nil];

}

 

@end

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值