创建四个控制器(Main)

本文详细介绍了如何在iOS应用中初始化自定义TabBarController,并通过代码优化实现统一设置UITabBarItem的字体颜色、图片及状态选择样式。同时,展示了如何添加多个子控制器并配置它们的UI元素,如图片、选中状态图片和标题。

// 第一次使用这个类或者子类的时候调用   // 设置控制器字体颜色
+ (void)initialize
{
    // self->WBTabBarController
    // 获取WBTabBarController里UITabBarItem
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    // 设置tabBar上按钮文字颜色:tabBar上的按钮是由对应的子控制器的tabBarItem模型
    // 注意以后想通过模型设置控件的颜色,通常我们都使用富文本属性
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [item setTitleTextAttributes:dict forState:UIControlStateSelected];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    // 添加所有子控制器
    [self setUpAllChildController];
    
}

// 代码优化思想:只要以后碰见一段功能,就抽出一个方法
#pragma mark - 添加所有的子控制器
- (void)setUpAllChildController
{
    // 添加子控制器
    // 首页
    WBHomeViewController *vc = [[WBHomeViewController alloc] init];
    [self setUpOneChildViewController:vc image:[UIImage imageNamed:@"tabbar_home"] selImg:[UIImage imageWithOriginalImageName:@"tabbar_home_selected"] title:@"首页"];
   
    
    // 消息
    WBMessageViewController *message = [[WBMessageViewController alloc] init];
    message.view.backgroundColor = [UIColor redColor];
    [self setUpOneChildViewController:message image:[UIImage imageNamed:@"tabbar_home"] selImg:[UIImage imageWithOriginalImageName:@"tabbar_home_selected"] title:@"首页"] ;
   
    
    // 发现
    WBDiscoverViewController *discover = [[WBDiscoverViewController alloc] init];
    [self setUpOneChildViewController:discover image:[UIImage imageNamed:@"tabbar_home"] selImg:[UIImage imageWithOriginalImageName:@"tabbar_home_selected"] title:@"首页"];
    
    // 我
    WBProfileViewController *profile = [[WBProfileViewController alloc] init];
    [self setUpOneChildViewController:profile image:[UIImage imageNamed:@"tabbar_home"] selImg:[UIImage imageWithOriginalImageName:@"tabbar_home_selected"] title:@"首页"] ;
 
}

// tabBarItem决定tabBar上的内容
- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selImg:(UIImage *)selImg title:(NSString *)title
{
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selImg;
 
 
    [self addChildViewController:vc];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值