自定义UITabBarController过程中遇到的那些神坑

以前写tabbar都是在AppDelegate.m里面写,这次看到别人将UITabBarController自定义,于是心血来潮,想着这是一种新的写法,就想试试,下面将写的过程中遇到的一些困惑和神坑记录下来。
1.新建一个UITabBarController的类,在.m文件里统一给TabbarItem设置属性,文字的大小,被选中的颜色等

/**
 * 设置item属性
 */
- (void)setupItem
{
    // UIControlStateNormal状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    // 文字颜色
    normalAttrs[NSForegroundColorAttributeName] = RGB(170, 170, 170);
    // 文字大小
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    
    // UIControlStateSelected状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    // 文字颜色
    selectedAttrs[NSFontAttributeName] = normalAttrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = RGB(249, 103, 80);
    
    // 统一给所有的UITabBarItem设置文字属性
    // 只有后面带有UI_APPEARANCE_SELECTOR的属性或方法, 才可以通过appearance对象来统一设置
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

2.自定义UITabBarController,设置其标题文字,默认的图片和被选中的图片

-(void)setupChildViewController:(UIViewController *)childController
                           title:(NSString *)title
                           image:(NSString *)image
                   selectedImage:(NSString *)selectedImage
{
    childController.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //[childController.tabBarItem setImage:[UIImage imageNamed:image]];
    //[childController.tabBarItem setSelectedImage:[UIImage imageNamed:selectedImage]];
    XCFNavViewController *nav = [[XCFNavViewController alloc] initWithRootViewController:childController];
    nav.title = title;
    [self addChildViewController:nav];
}


实现这个方法
-(void)createChildTabBar

{
    [self setupChildViewController:[[XCFKitchenViewController alloc] init]
                             title:@"下厨房"
                             image:@"tabADeselected"
                     selectedImage:@"tabASelected"];
    
    [self setupChildViewController:[[XCFBazaarController alloc] init]
                             title:@"市集"
                             image:@"tabBDeselected"
                     selectedImage:@"tabBSelected"];
    [self setupChildViewController:[[XCFCommunityViewController alloc] init]
                             title:@"社区"
                             image:@"tabCDeselected"
                     selectedImage:@"tabCSelected"];
    [self setupChildViewController:[[XCFMeController alloc] init]
                             title:@"我"
                             image:@"tabDDeselected"
                     selectedImage:@"tabDSelected"];
    
}

就这样,一个自定义的tabbar就基本写成了。最后,需要在AppDelegate.m里面设置一下rootcontrller,
self.window.rootViewController = [[XCFTabBarController alloc] init];
就这样,一个大致的模子就出来了,下面上图

自定义UITabBarController过程中遇到的那些神坑_第1张图片
Paste_Image.png

下面需要注意的两个问题,第一个问题是,一定要将图片设置正确,不然会出现
下图所示的情况


Paste_Image.png

会出现一个很大很难看的图标,出现这个的原因是图片的格式没有设置对,一定要将图片的格式设置为@2x,这样才可以。
第二个问题是ios7以后,如果自定义的话,需要设置图片的渲染模式,不然是不会显示原图的,苹果默认会显示蓝色的,如下图所示

Paste_Image.png

出现了这个,就是因为没有选中图片的渲染模式,苹果默认就显示蓝色了,这里我们要设置图片不渲染,也就是始终保持原来的样子。

childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

这样就可以了。

你可能感兴趣的:(自定义UITabBarController过程中遇到的那些神坑)