CustomTabBarController

.h

#import <UIKit/UIKit.h>

@interface ECGCustomTabBarController : UITabBarController

//注意:数组是根据实际tabbar的视图控制器顺序创建的,顺序不能乱; 而接下来的normalImageNames和disabledImageNames与视图控制器数组三者之间都是一一对应,不能错序;   num是Tabbar需要展示几个视图控制器
@property(nonatomic,assign)int countNum;
@property(nonatomic, strong)NSArray *contentViewControllers;//需要展示的视图控制器数组
@property(nonatomic, strong)NSArray *normalImageNames;//正常Tabbar的items图片数组
@property(nonatomic, strong)NSArray *disabledImageNames;禁用状态Tabbar的items图片数组
@property(nonatomic, strong)NSArray *titles;//Tabbar的items标题数组
@property(nonatomic, assign)int indexTabBarButton;//选中Tabbar的某个索引下的按钮
@property(nonatomic, strong)UILabel *badgeLabel;//角标
/**
 *  <#Description#>
 *
 *  @param b <#b description#>
 */

//NO.1 --->配合属性使用
- (void)showTabbar;

//NO.2
- (void)getWithViewControllers:(NSArray *)viewControllers number:(int)num normalImageNames:(NSArray *)normalImageNames disabledImageNames:(NSArray *)disabledImageNames titles:(NSArray *)titles;

//隐藏
- (void)hideBottomBar:(BOOL)b;
@end


.m

#import "ECGCustomTabBarController.h"
#import "ECGTabBarButton.h"
#import "ECGConst.h"
#import "ECGMacro.h"


@interface ECGCustomTabBarController (){
    UIImageView *_tabBarView; //自定义的覆盖原先的tarbar的控件
    
    ECGTabBarButton *_previousBtn; //记录前一次选中的按钮
    UIView*bottomView;
    NSArray *controllers;
    NSArray *normalImages;
    NSArray *disabledImages;
    NSArray *tabButtonTitles;
    int number;
    NSMutableArray *navArray;//存放创建好的导航控制器
}

@end

@implementation ECGCustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)getWithViewControllers:(NSArray *)viewControllers number:(int)num normalImageNames:(NSArray *)normalImageNames disabledImageNames:(NSArray *)disabledImageNames titles:(NSArray *)titles{
    controllers = viewControllers;
    normalImages = normalImageNames;
    disabledImages = disabledImageNames;
    tabButtonTitles = titles;
    number = num;
    [self loadTabBar];
    [self loadVC];
    
}

- (void)setCountNum:(int)countNum{
    if (countNum >= 0) {
        _countNum = countNum;
        number = _countNum;
    }
}


- (void)setContentViewControllers:(NSArray *)contentViewControllers{
    if (contentViewControllers && [contentViewControllers count] > 0) {
        _contentViewControllers = contentViewControllers;
        controllers = _contentViewControllers;
    }
}

- (void)setNormalImageNames:(NSArray *)normalImageNames{
    if (normalImageNames && [normalImageNames count] > 0) {
        _normalImageNames = normalImageNames;
        normalImages = _normalImageNames;
    }
}

- (void)setDisabledImageNames:(NSArray *)disabledImageNames{
    if (disabledImageNames && [disabledImageNames count] > 0) {
        _disabledImageNames = disabledImageNames;
        disabledImages = _disabledImageNames;
    }
}

- (void)setTitles:(NSArray *)titles{
    if (titles && titles.count > 0) {
        _titles = titles;
        tabButtonTitles = _titles;
    }
}

- (void)setIndexTabBarButton:(int)indexTabBarButton{
    if (indexTabBarButton >= 0) {
        _indexTabBarButton = indexTabBarButton;
        if (_tabBarView) {
            ECGTabBarButton *btn = _tabBarView.subviews[_indexTabBarButton];
            [self changeViewController:btn]; //自定义的控件中的按钮被点击了调用的方法,自定义跳转
        }
    }
}

- (void)showTabbar{
    [self loadTabBar];
    [self loadVC];
}


- (void)loadTabBar{
    if ([controllers count]<=0) {
        return;
    }
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBar.hidden = YES; //隐藏原先的tabBar
    CGFloat tabBarViewY = self.view.frame.size.height - TabBarHeight;
    
    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, tabBarViewY+1.5, SCREEN_WIDTH, TabBarHeight)];
    _tabBarView.userInteractionEnabled = YES; //这一步一定要设置为YES,否则不能和用户交互
    _tabBarView.backgroundColor = RGBACOLOR(233, 230, 225, 1.0);
    bottomView=[[UIView alloc]initWithFrame:CGRectMake(0, tabBarViewY, SCREEN_WIDTH, 1.5)];
    bottomView.backgroundColor=[UIColor lightGrayColor];
    [self.view addSubview:bottomView];
    
    [self.view addSubview:_tabBarView];
    
    // 下面的方法是调用自定义的生成按钮的方法
    navArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i<number; i++) {
        [self creatButtonWithNormalName:normalImages[i] andSelectName:disabledImages[i] andTitle:tabButtonTitles[i] andIndex:i];
    }
    
    
    ECGTabBarButton *btn = _tabBarView.subviews[_indexTabBarButton];
    
    [self changeViewController:btn]; //自定义的控件中的按钮被点击了调用的方法,默认进入界面就选中第一个按钮
    
//    [[UIApplication sharedApplication].keyWindow addSubview:self.view];
//    [UIApplication sharedApplication].keyWindow.rootViewController = self;
}

#pragma mark 创建一个tab按钮
- (void)creatButtonWithNormalName:(NSString *)normal andSelectName:(NSString *)selected andTitle:(NSString *)title andIndex:(int)index
{
    ECGTabBarButton *button = [ECGTabBarButton buttonWithType:UIButtonTypeCustom];
    button.tag = index;
    
    CGFloat buttonW = _tabBarView.frame.size.width / number;
    CGFloat buttonH = _tabBarView.frame.size.height;
    button.frame = CGRectMake(buttonW *(index), 0, buttonW, buttonH);
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = SystemFont(20);
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selected] forState:UIControlStateDisabled];//禁用状态
   
    [button setTitle:title forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchDown];
    
    button.imageView.contentMode = UIViewContentModeCenter; // 让图片在按钮内居中
    [button setTitleColor:RGBACOLOR(98, 192, 163, 1) forState:UIControlStateDisabled];
    button.titleLabel.textAlignment = NSTextAlignmentCenter; // 让标题在按钮内居中
    button.titleLabel.font = SystemFont(12);
    
    [_tabBarView addSubview:button];
    if (button.tag == 2) {
        //获取图片大小
        UIImage *im = [UIImage imageNamed:normal];
        CGFloat x = buttonW-(buttonW-im.size.width)/2-2;
        _badgeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, 0, 10, 10)];
        _badgeLabel.backgroundColor = [UIColor clearColor];
        _badgeLabel.layer.cornerRadius = 5.0f;
        _badgeLabel.layer.masksToBounds = YES;
        [button addSubview:_badgeLabel];
    }
    
}

/**
 *  <#Description#>
 */
- (void)loadVC{
    
    //    ECGChatViewController *chatController = [[ECGChatViewController alloc] initWithNibName:@"ECGChatViewController" bundle:nil];
    for (int j = 0; j<number; j++) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controllers[j]];
        [navArray addObject:nav];
    }
    
    self.viewControllers = [NSArray arrayWithArray:navArray];
    
    
}

#pragma mark 按钮被点击时调用
/**
 *  <#Description#>
 *
 *  @param sender <#sender description#>
 */
- (void)changeViewController:(ECGTabBarButton *)sender
{
    self.selectedIndex = sender.tag; //切换不同控制器的界面
    sender.backgroundColor = RGBACOLOR(233, 230, 225, 1.0);
    if (_previousBtn) {
        _previousBtn.backgroundColor = RGBACOLOR(233, 230, 225, 1.0);
    }
    sender.enabled = NO;
    
    if (_previousBtn != sender) {
        
        _previousBtn.enabled = YES;
        
    }
    _previousBtn = sender;
}

//- (void)setSelectedIndex:(NSUInteger)selectedIndex{
//    [super setSelectedIndex:selectedIndex];
//}


/**
 *  <#Description#>
 *
 *  @param b <#b description#>
 */
-(void)hideBottomBar:(BOOL)b {
    //_tabBarView.hidden = b;
    if (b) {
        [_tabBarView removeFromSuperview];
        [bottomView removeFromSuperview];
    }
    else {
        [self.view addSubview:_tabBarView];
        [self.view addSubview:bottomView];
    }
}


/*
#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.
}
*/

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

@end


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,展示了如何生成一个自定义的TabBar: 首先,创建一个名为CustomTabBarController的自定义视图控制器,作为TabBar的容器: ```swift import UIKit class CustomTabBarController: UIViewController { // 自定义TabBar视图 let customTabBar = CustomTabBar() override func viewDidLoad() { super.viewDidLoad() // 添加自定义TabBar视图 view.addSubview(customTabBar) // 设置自定义TabBar的位置和大小 customTabBar.frame = CGRect(x: 0, y: view.frame.height - 100, width: view.frame.width, height: 100) // 设置自定义TabBar的按钮点击事件 customTabBar.button1.addTarget(self, action: #selector(button1Tapped), for: .touchUpInside) customTabBar.button2.addTarget(self, action: #selector(button2Tapped), for: .touchUpInside) customTabBar.button3.addTarget(self, action: #selector(button3Tapped), for: .touchUpInside) } // 按钮1点击事件 @objc func button1Tapped() { // 切换到第一个视图控制器 selectedIndex = 0 } // 按钮2点击事件 @objc func button2Tapped() { // 切换到第二个视图控制器 selectedIndex = 1 } // 按钮3点击事件 @objc func button3Tapped() { // 切换到第三个视图控制器 selectedIndex = 2 } } ``` 接下来,创建一个名为CustomTabBar的自定义TabBar视图,用于显示TabBar按钮: ```swift import UIKit class CustomTabBar: UIView { // TabBar按钮 let button1 = UIButton() let button2 = UIButton() let button3 = UIButton() override init(frame: CGRect) { super.init(frame: frame) // 设置按钮的样式、位置和大小 button1.setTitle("Tab 1", for: .normal) button1.frame = CGRect(x: 0, y: 0, width: frame.width / 3, height: frame.height) button2.setTitle("Tab 2", for: .normal) button2.frame = CGRect(x: frame.width / 3, y: 0, width: frame.width / 3, height: frame.height) button3.setTitle("Tab 3", for: .normal) button3.frame = CGRect(x: (frame.width / 3) * 2, y: 0, width: frame.width / 3, height: frame.height) // 添加按钮到自定义TabBar视图 addSubview(button1) addSubview(button2) addSubview(button3) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } ``` 最后,在AppDelegate中设置CustomTabBarController为应用程序的主视图控制器: ```swift import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) // 创建自定义TabBar视图控制器 let customTabBarController = CustomTabBarController() // 设置自定义TabBar视图控制器为主视图控制器 window?.rootViewController = customTabBarController window?.makeKeyAndVisible() return true } } ``` 在这个示例中,我们创建了一个CustomTabBarController作为自定义TabBar的容器,并在其中添加了CustomTabBar视图。CustomTabBar视图中包含了三个按钮,分别用于切换到不同的视图控制器。你可以根据需要进行修改和扩展,以满足你的具体需求。 希望对你有所帮助!如果你有任何进一步的问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值