在一个APP中使用多个tabBar

入口的 UITabBarController【A】

创建方式和正常方式一样,可以使用代码,也可以使用storyboard创建

功能视图里的 UITabBarController【B】

我这里选择使用代码实现,因为我要自定义B的tabBarItem

1.Item是自定义Button,取消点击效果,创建自己的Button

MyTabBarBtn.h
#import <UIKit/UIKit.h>

@interface MyTabBarBtn : UIButton

@end
MyTabBarBtn.m
#import "MyTabBarBtn.h"

@implementation MyTabBarBtn

/* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */

- (void)setHighlighted:(BOOL)highlighted {
    // 只需要重写就可以取消点击效果,不用添加内容
}

@end

2.创建自己的 UITabBarController

CusTabBarController.h
#import <UIKit/UIKit.h>

@interface CusTabBarController : UITabBarController

@end
CusTabBarController.m
#import "CusTabBarController.h"
#import "CusViewController.h"
#import "ReCallViewController.h"

#import "MyTabBarBtn.h"

@interface CusTabBarController () <UITabBarControllerDelegate>
{
    MyTabBarBtn * _btnLeft;
    MyTabBarBtn * _btnRight;
}
@end

@implementation CusTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 先添加视图控制器
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"faxingshi" bundle:nil];
    CusViewController * cusVC = [story instantiateViewControllerWithIdentifier:@"cusVC"];
    ReCallViewController * reCall = [story instantiateViewControllerWithIdentifier:@"reCallVC"];
    self.viewControllers = @[cusVC,reCall];

    // 后添加按钮
    UITabBar *tabBar = self.tabBar;
    tabBar.barTintColor = [UIColor clearColor];
    tabBar.backgroundColor = [UIColor clearColor];

// UITabBarItem * leftBarItem = [tabBar.items objectAtIndex: 0];
// UITabBarItem * rightBarItem = [tabBar.items objectAtIndex: 1];
//
// 
// leftBarItem.selectedImage = [UIImage imageNamed: @""];
// rightBarItem.image = [UIImage imageNamed: @""];
// 
// leftBarItem.selectedImage = [UIImage imageNamed: @""];
// rightBarItem.image = [UIImage imageNamed: @""];

    _btnLeft = [[MyTabBarBtn alloc] init];
    _btnLeft.frame = CGRectMake(0, 0, SCREEN_WIDTH/2, tabBar.frame.size.height);
    [_btnLeft setImage:[[UIImage imageNamed:@"cusBarIcon1Nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_btnLeft setImage:[[UIImage imageNamed:@"cusBarIcon1Sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
    [_btnLeft setTitle:@"今日回访" forState:UIControlStateNormal];
    [_btnLeft setTitleColor:[UIColor colorWithHexString:@"a2a4a7"] forState:UIControlStateNormal];
    [_btnLeft setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [_btnLeft setBackgroundImage:[[UIImage imageNamed:@"btnNorBG"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_btnLeft setBackgroundImage:[[UIImage imageNamed:@"btnSelBG"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
    [_btnLeft addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [tabBar addSubview:_btnLeft];

    _btnRight = [[MyTabBarBtn alloc] init];
    _btnRight.frame = CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, tabBar.frame.size.height);
    [_btnRight setImage:[[UIImage imageNamed:@"cusBarIcon2Nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_btnRight setImage:[[UIImage imageNamed:@"cusBarIcon2Sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
    [_btnRight setTitle:@"我的客户" forState:UIControlStateNormal];
    [_btnRight setTitleColor:[UIColor colorWithHexString:@"a2a4a7"] forState:UIControlStateNormal];
    [_btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [_btnRight setBackgroundImage:[[UIImage imageNamed:@"btnNorBG"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [_btnRight setBackgroundImage:[[UIImage imageNamed:@"btnSelBG"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
    [_btnRight addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [tabBar addSubview:_btnRight];

    // 默认显示今日回访
    _btnLeft.selected = YES;


}
// 切换视图控制器
- (void)btnClicked:(UIView *)sender {

    if ([sender isEqual:_btnLeft]) {
        if (!_btnLeft.selected) {
            self.selectedIndex = 0;
            _btnRight.selected = NO;
            _btnLeft.selected = YES;
        }

    } else if ([sender isEqual:_btnRight]) {
        self.selectedIndex = 1;
        _btnLeft.selected = NO;
        _btnRight.selected = YES;
    }

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */

@end

3.从A的某个入口进入到B的视图控制器的方法

// 此时必须要将 navigationBar 和 tabBar 隐藏
[self.navigationController.navigationBar setHidden:YES];
[self.tabBarController.tabBar setHidden:YES];
CusTabBarController * cusBarVC = [[CusTabBarController alloc] init];
[self.navigationController pushViewController:cusBarVC animated:YES];

如果入口所在的视图控制器在之前的一个tabbr里,那么必须在入口的视图控制器的 viewWillAppear 函数中,否则切换回来的时候 tabBar 会隐藏(因为跳转的时候设置隐藏了)

[self.tabBarController.tabBar setHidden:NO];

4.从B(UITabBarController)的视图控制器返回到A的视图控制器里的方法

[self.tabBarController.navigationController popToRootViewControllerAnimated:YES];

UITabBarController A
在一个APP中使用多个tabBar_第1张图片

UITabBarController B
在一个APP中使用多个tabBar_第2张图片

你可能感兴趣的:(APP,界面)