UITabBarController的使用

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

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

1、视图控制器集合:viewControllers

2、切换视图属性:tabbar

2-1、tabbar背景颜色:backgroundColor

2-2、tabbar背景图标:backgroundImage

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

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

或进行 2-3、2-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]];

 

6、tabbar标题设置后出现偏移情况,即靠近底端,可通过设置属性 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 标题、图标
// 方法1
NSArray *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;
}

 

// 方法2
UITabBarItem *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];
    }
}

 

注意:

在使用过程中,如果某个一视图控制器,需要诸如登录状态才能使用的,这时候在点击tabBar中的item时,就需要进行判断登录状态。具体实现可以通过tabBar的代理方法实现。

 

// 代理对象 
self.tabBarController.delegate = self;

// 协议 UITabBarControllerDelegate

// 代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    // 是否登录状态,被点击的是否需要登录状态的视图
    if ([viewController.title isEqualToString:@"xx视图"] && isLogin)
    {
        // 如果未登录,且点击视图是需要登录状态的,则不跳转
        return NO;
    }
    return YES;
}

 

 

 

 

 

 

 

 

 


UITabBarController的使用_第1张图片   UITabBarController的使用_第2张图片

 

 

 

 

 

UITabBarController的使用_第3张图片   UITabBarController的使用_第4张图片

 

 

使用自定义的消息提示

 

- (void)showBadge:(NSInteger)number atIndex:(NSInteger)index
{
    UILabel *badgeView = (UILabel *)[self.tabBar viewWithTag:(tagBadgeView + index)];
    if (badgeView && [badgeView isKindOfClass:[UILabel class]])
    {
        badgeView.hidden = NO;
    }
    else
    {
        float percentX = (index + 0.6) / self.controllers.count;
        CGFloat originX = ceilf(percentX * self.tabBar.size.width);
        CGFloat originY = ceilf(0.1 * self.tabBar.size.height);
        CGFloat size = 16.0;
        UILabel *badgeView = [[UILabel alloc] initWithFrame:CGRectMake(originX, originY, size, size)];
        badgeView.backgroundColor = [UIColor redColor];
        badgeView.layer.cornerRadius = size / 2;
        badgeView.layer.masksToBounds = YES;
        badgeView.tag = (index + tagBadgeView);
        badgeView.hidden = NO;
        badgeView.font=kFontSize10;
        badgeView.textAlignment=NSTextAlignmentCenter;
        badgeView.textColor=kColorWhite;
        [self.tabBar addSubview:badgeView];
    }
    if(number>99)
    {
        badgeView.text=@"99";
    }
    else if(number>0)
    {
        badgeView.text=[NSString stringWithFormat:@"%d",(int)number];
    }
    else
    {
        badgeView.text=@"";
    }
}
- (void)hiddenBadgeAtIndex:(NSInteger)index
{
    UILabel *badgeView = (UILabel *)[self.tabBar viewWithTag:(tagBadgeView + index)];
    if (badgeView && [badgeView isKindOfClass:[UILabel class]])
    {
        badgeView.hidden = YES;
    }
}

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(iOS,视图控制器,iOS,开发编码收集)