美文网首页
UITabBarController

UITabBarController

作者: CoderRH | 来源:发表于2016-08-10 16:49 被阅读13次
    • UITabBarController添加控制器的方式有2种
    • 添加单个子控制器
       - (void)addChildViewController:(UIViewController *)childController;
    
    • 设置子控制器数组
    @property(nonatomic,copy) NSArray *viewControllers;
    

    UITabBarButton

    UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem属性决定

    //UITabBarItem有以下属性影响着UITabBarButton的内容
    //标题文字
    @property(nonatomic,copy) NSString *title;
    
    //图标
    @property(nonatomic,retain) UIImage *image;
    
    //选中时的图标
    @property(nonatomic,retain) UIImage *selectedImage;
    
    //提醒数字
    @property(nonatomic,copy) NSString *badgeValue;
    

    自定义tabbar相关

    • 替换UITabBarController内部的tabBar
    // 这里的self是UITabBarController
    [self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];
    
    • 自定义tabbar实现中间多出一个加号的发布按钮
    /**** 设置中间的发布按钮的frame ****/
    
        //先设置宽、高 再设置中心点 变换顺序会导致按钮往右下方向偏移
        CGRect frame = self.otherButton.frame;
        frame.size = CGSizeMake(buttonW, buttonH);
        self.otherButton.frame = frame;
    
        self.otherButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    
        //直接这样设置frame会导致按钮往右下方向偏移
    //    self.otherButton.frame =  CGRectMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5, buttonW, buttonH);
    

    设置TabBarItem的文字属性

    • 直接设置每一个tabBarItem对象
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    [vc.tabBarItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [vc.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    // 字典中用到的key
    1.iOS7之前(在UIStringDrawing.h中可以找到)
    - 比如UITextAttributeFont\UITextAttributeTextColor
    - 规律:UITextAttributeXXX
    
    2.iOS7开始(在NSAttributedString.h中可以找到)
    - 比如NSFontAttributeName\NSForegroundColorAttributeName
    - 规律:NSXXXAttributeName
    
    • 通过UITabBarItem的appearance对象统一设置
    /**** 设置所有UITabBarItem的文字属性 ****/
    UITabBarItem *item = [UITabBarItem appearance];
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateSelected];
    

    相关文章

      网友评论

          本文标题:UITabBarController

          本文链接:https://www.haomeiwen.com/subject/scjjsttx.html