美文网首页
iOS开发 - UITabBarController 标签栏的设

iOS开发 - UITabBarController 标签栏的设

作者: VitasLiu | 来源:发表于2016-11-28 19:18 被阅读0次

    1.每个UIViewController都有一个tabBarItem

    @interface UIViewController (UITabBarControllerItem)
    
    @property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem;
    

    2.UITabBarItem(子类)

    NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem
    

    3.UIBarItem(父类)

    NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject <NSCoding, UIAppearance>
    // 设置tabBarItem的文字
    @property(nullable, nonatomic,copy)             NSString    *title;        // default is nil
    

    4.示例代码

    • 示例代码1
    //创建子控制器
    UIViewController *c1=[[UIViewController alloc]init];
    c1.view.backgroundColor=[UIColor grayColor];
    c1.view.backgroundColor=[UIColor greenColor];
    c1.tabBarItem.title=@"消息";
    c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
    c1.tabBarItem.badgeValue=@"123";
    
    • 示例代码2
    /**
     往tabbar添加一个子控制器
    
     @param childController 子控制器
     @param title 子控制器的title
     @param image tabbar的图片名字
     */
    - (void)addChildViewController:(UIViewController *)childController title: (NSString *)title image: (NSString *)image {
        childController.title = title;
        
        // 设置tabbarItem的文字大小和颜色
        [childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: globalColor} forState:UIControlStateSelected];
        [childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];
        
        // 设置tabbar的图片
        // 如果没有传图片(中间的占位控制器), 就不设置tabbar的image
        if (image != nil) {
            childController.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@", image]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"tabbar_%@_selected", image]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
        
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:childController];
        
        [self addChildViewController:navController];
    }
    
    
    • 示例代码3
    
    // 设置tabbarItem的文字大小和颜色
    [childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: globalColor} forState:UIControlStateSelected];
    [childController.tabBarItem setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:11], NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];
    
    
    • 很多属性的设置都可以在这个文件里找到

    NSAttributedString.h

    5.添加子控制器到UITabBarController中

    • 方式一
    [tb addChildViewController:c1];
    [tb addChildViewController:c2];
    
    • 方式二
    tb.viewControllers=@[c1,c2,c3,c4];
    

    6.UITabBar
    下方的工具条称为UITabBar ,如果UITabBarController有N个子控制器,那么UITabBar内部就会有N 个UITabBarButton作为子控件与之对应。

    注意:UITabBarButton在UITabBar中得位置是均分的,UITabBar的高度为49

    UITabBar UITabBarItem的设置

    相关文章

      网友评论

          本文标题:iOS开发 - UITabBarController 标签栏的设

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