UITabBarController的使用

来源:互联网 发布:与sqlserver建立连接 编辑:程序博客网 时间:2024/04/30 04:04

UITabBarController 是多页面视图控制器切换控制器

一、主要使用方法,即属性设置:

1、视图控制器集合:viewControllers

2、切换视图属性:tabbar

2-1tabbar背景颜色:backgroundColor

2-2tabbar背景图标:backgroundImage

2-3tabbar标题(选中,或非选中状态):UITabBarItem- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state

2-4tabbar图标(选中,或非选中状态):UITabBarItem- (void)setFinishedSelectedImage:(nullable UIImage *)selectedImage withFinishedUnselectedImage:(nullable UIImage *)unselectedImage

或进行 2-32-4属性的统一设置方法如:

// 设置标题,未选中状态图标,选中状态图标UITabBarItem *barItem = [[UITabBarItem alloc] initWithTitle:title image:imageNormal selectedImage:imageSelected];xxxViewController.tabBarItem = barItem;
// 设置标题字体颜色[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
// 设置标题字体大小[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];
// 设置标题字体偏移[[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];
// 设置图标选中时颜色[[UITabBar appearance] setTintColor:[UIColor redColor]];

3、默认选中视图控制器:selectedIndex

     

二、使用注意事项:

1、设置视图控制器集合时,是 UINavigationController导航栏控制器集合;

2、设置视图控制器集合时,通常小于等于 5视图控制器,超过 5个时系统默认生成一个 more的控制器页面,用于操作多余的视图控制器

3、视图控制器导航栏标题设置时,注意使用:" self.navigationItem.title = @"xxx"; ",而不使用" self.title = @"xxx"; ",避免影响 UITabBarController tabbar标题的设置。

4、当要显示下一个视图控制器,且需要隐藏 tabbarController控制器时,设置视图控制器的属性 hidesBottomBarWhenPushed值为YES,如:

UIViewController *nextVC = [[UIViewController alloc] init];nextVC.hidesBottomBarWhenPushed = YES;[self.navigationController pushViewController:nextVC animated:YES];

5、第一次初始化时,会出现设置的 tabbar图标颜色异常,设置属性 tintColor与图标颜色一致后,则不会,如:

[[UITabBar appearance] setTintColor:[UIColor redColor]];

6tabbar标题设置后出现偏移情况,即靠近底端,可通过设置属性 titlePositionAdjustment进行调整,如:

[[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];

7、设置 badgeValue标识属性时,特别是在 viewController中设置时,注意使用方法为" self.navigationController.tabBarItem.badgeValue = @"0"; ",而不是" self.tabBarItem.badgeValue = @"0"; ",否则无效,如:

- (void)viewWillAppear:(BOOL)animated{        NSInteger index = arc4random() % 2;        if (0 == index)        {            // self.tabBarItem.badgeValue = nil; // 无效            self.navigationController.tabBarItem.badgeValue = nil;        }        else        {            index = arc4random() % 100 + 1;            // self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", index]; // 无效            self.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld", index];        }}

// 1 创建视图控制器MessageViewController *messageVC = [[MessageViewController alloc] init];UINavigationController *messageNav = [[UINavigationController alloc] initWithRootViewController:messageVC];ContacterViewController *contacterVC = [[ContacterViewController alloc] init];UINavigationController *contacterNav = [[UINavigationController alloc] initWithRootViewController:contacterVC];DynamicViewController *dynamicVC = [[DynamicViewController alloc] init];UINavigationController *dynamicNav = [[UINavigationController alloc] initWithRootViewController:dynamicVC];// 视图控制器数组NSArray *controllerArray = @[messageNav, contacterNav, dynamicNav];

// 2 创建tabbarController控制器UITabBarController *tabbarController = [[UITabBarController alloc] init];// 属性设置// 设置默认被选中视图控制器tabbarController.selectedIndex = 0;// 设置切换视图 tabBar 属性// 1 打开用户交互tabbarController.tabBar.userInteractionEnabled = YES;// 2 设置背景颜色tabbarController.tabBar.backgroundColor = [UIColor whiteColor];// 3 设置背景图片tabbarController.tabBar.backgroundImage = [UIImage imageNamed:@"background"];// 选中时的背景图片tabbarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"backgroundSelected"];// 4 设置按钮标题、常规图标、选中时图标NSArray *titleArray = @[@"消息", @"联系人", @"动态"];NSArray *imageNArray = @[[UIImage imageNamed:@"messageNormal"], [UIImage imageNamed:@"contacterNormal"], [UIImage imageNamed:@"dynamicNormal"]];NSArray *imageSAarray = @[[UIImage imageNamed:@"messageSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"dynamicSelected"]];


/**************************************/    // 更多视图控制器的情况UIViewController *vc4 = [[UIViewController alloc] init];vc4.title = @"vc4";UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];UIViewController *vc5 = [[UIViewController alloc] init];vc5.title = @"vc5";UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:vc5];UIViewController *vc6 = [[UIViewController alloc] init];vc6.title = @"vc6";UINavigationController *nav6 = [[UINavigationController alloc] initWithRootViewController:vc6];UIViewController *vc7 = [[UIViewController alloc] init];vc7.title = @"vc7";UINavigationController *nav7 = [[UINavigationController alloc] initWithRootViewController:vc7];UIViewController *vc8 = [[UIViewController alloc] init];vc8.title = @"vc8";UINavigationController *nav8 = [[UINavigationController alloc] initWithRootViewController:vc8];// 视图控制器数组NSArray *controllerArray = @[messageNav, contacterNav, dynamicNav, nav4, nav5, nav6, nav7, nav8];NSArray *titleArray = @[@"消息", @"联系人", @"动态", @"nav4", @"nav5", @"nav6", @"nav7", @"nav8"];NSArray *imageNArray = @[[UIImage imageNamed:@"messageNormal"], [UIImage imageNamed:@"contacterNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"], [UIImage imageNamed:@"dynamicNormal"]];NSArray *imageSAarray = @[[UIImage imageNamed:@"messageSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"dynamicSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"], [UIImage imageNamed:@"contacterSelected"]];    /**************************************/

// 5 设置视图控制器tabbarController.viewControllers = controllerArray;

// 6 设置 tabbar 标题、图标// 方法1NSArray *items = tabbarController.tabBar.items;NSInteger count = items.count;for (int index = 0; index < count; index++){        NSString *title = titleArray[index];        UIImage *imageN = imageNArray[index];        UIImage *imageS = imageSAarray[index];                UITabBarItem *aItem = [items objectAtIndex:index];        // 标题设置(字体偏移、字体大小、字体颜色)        aItem.title = title;//        aItem.titlePositionAdjustment = UIOffsetMake(0.0, -10.0);//        [aItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];//        [aItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];//        [aItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];        // 图标设置//        [aItem setFinishedSelectedImage:imageS withFinishedUnselectedImage:imageN];        aItem.image = imageN;        aItem.selectedImage = imageS;}

// 方法2UITabBarItem *messageBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[0] image:imageNArray[0] selectedImage:imageSAarray[0]];[messageBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];[messageBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];messageBarItem.badgeValue = @"10";messageVC.tabBarItem = messageBarItem;UITabBarItem *contacterBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[1] image:imageNArray[1] selectedImage:imageSAarray[1]];[contacterBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];[contacterBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];contacterBarItem.badgeValue = @"3";contacterVC.tabBarItem = contacterBarItem;UITabBarItem *dynamicBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[2] image:imageNArray[2] selectedImage:imageSAarray[2]];[dynamicBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateNormal];[dynamicBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];dynamicBarItem.badgeValue = @"21";dynamicVC.tabBarItem = dynamicBarItem;


// Item显示样式设置// 设置字体颜色[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateNormal];[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];// 设置字体大小[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:8.0]} forState:UIControlStateNormal];// 设置字体偏移[[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0, -8.0)];// 设置图标选中时颜色[[UITabBar appearance] setTintColor:[UIColor redColor]];// 背景图标[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"xxx"]];

// 方法3// 添加子控制器- (void)initWithViewController:(NSArray *)controllers titles:(NSArray *)titles imageNormal:(NSArray *)imageNorNames imageSelected:(NSArray *)imageSelNames{    NSAssert(controllers != nil, @"controllers must be not nil");    NSAssert(titles != nil, @"titles must be not nil");    NSAssert(imageNorNames != nil, @"imageNorNames must be not nil");    NSAssert(imageSelNames != nil, @"imageSelNames must be not nil");    if (controllers.count != titles.count || controllers.count != imageNorNames.count || controllers.count != imageSelNames.count)    {        NSLog(@"%s error:数量不相等。", __FUNCTION__);        return;    }        for (int index = 0; index < controllers.count; index++)    {        UIViewController *controller = controllers[index];        NSString *title = titles[index];        NSString *imageNormal = imageNorNames[index];        NSString *imageSelected = imageSelNames[index];                controller.title = title;        controller.tabBarItem.image = [UIImage imageNamed:imageNormal];        controller.tabBarItem.selectedImage = [UIImage imageNamed:imageSelected];        // 导航视图控制器        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];                [self addChildViewController:nav];    }}




   


   






0 0
原创粉丝点击