iOS 自定义tabBar


苹果虽然帮我们封装好了菜单栏,但是有些时候我们不想用封装好的怎么办?自定义吧!!

直接上代码了!!

<span style="color:#ff0000;">HFG_TabBar.h</span>

#import <UIKit/UIKit.h>

@interface HFG_TabBar : UITabBarController

@end

<span style="color:#ff0000;">HFG_TabBar.m</span>



#import "HFG_TabBar.h"

@interface HFG_TabBar ()
{
    UIButton *btn;
}
@property (nonatomic,strong)UIButton *selectedBtn;
@end

@implementation HFG_TabBar

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

- (void) viewWillLayoutSubviews{
    
    [self.tabBarController.tabBar removeFromSuperview]; // 移除原先的tabBar
    CGFloat tabBarY = [UIScreen mainScreen].bounds.size.height - 49;
    UIView *tabBarView = [[UIView alloc]initWithFrame:CGRectMake(0, tabBarY, [UIScreen mainScreen].bounds.size.width, 49)];
//    视图设置相关都在这里
    [tabBarView setBackgroundColor:[UIColor colorWithRed:0.748 green:0.850 blue:1.000 alpha:1.000]];
    [self.view addSubview:tabBarView];
    for (int i = 0; i < 2; i ++) {
        btn = [[UIButton alloc]init];
        
        //给按钮添加图片 没有图片就没加
//                NSString *imageName = [NSString stringWithFormat:@"HFG_TabBar%d", i ];
//                NSString *imageNameSel = [NSString stringWithFormat:@"HFG_TabBar%dSelect", i ];
//                [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//                [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected];
//                [btn setBackgroundColor:[UIColor greenColor]];

        if (i == 0) {
            [btn setTitle:@"标题1" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected
             ];
            [btn setBackgroundColor:[UIColor colorWithRed:0.276 green:1.000 blue:0.538 alpha:1.000]];
        }
        if (i == 1) {
            [btn setTitle:@"标题2" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
            [btn setBackgroundColor:[UIColor colorWithRed:0.120 green:0.538 blue:1.000 alpha:1.000]];
        }
        CGFloat X = i *tabBarView.frame.size.width/2;
        btn.frame = CGRectMake(X, 0, tabBarView.frame.size.width/2, tabBarView.frame.size.height);
        [tabBarView addSubview:btn];
        //   设置刚进入时,第一个按钮为选中状态
        if (0 == i) {
            btn.selected = YES;
            self.selectedBtn = btn; // 设置该按钮为选中的按钮
        }
        btn.tag = i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void) clickBtn:(UIButton *)sender{
    
    if (sender.tag == 0) {
        self.selectedIndex = 0;
    }
    if (sender.tag == 1) {
        self.selectedIndex = 1;
    }
    self.selectedBtn.selected = NO;
    sender.selected = YES;
    self.selectedBtn = sender;
    
}

@end
这样,我们的自定义tabBar就定义好了,但重要的是,怎么和视图控制器关联起来?其实很简单,先创建好相关的视图控制器,然后代码如下:

<span style="color:#ff0000;">AppDelegate.m</span>

#import "AppDelegate.h"
#import "HFG_TabBar.h"
#import "HFG_RandomCallViewController.h"
#import "HFG_AllClassViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    HFG_RandomCallViewController *RandomCall = [[HFG_RandomCallViewController alloc]init];
    HFG_AllClassViewController *AllClass = [[HFG_AllClassViewController alloc]init];
    
    UINavigationController *randomCall = [[UINavigationController alloc]initWithRootViewController:RandomCall];
    UINavigationController *allClass = [[UINavigationController alloc]initWithRootViewController:AllClass];
 
    HFG_TabBar *tabBarController = [[HFG_TabBar alloc]init];
    
    tabBarController.viewControllers = @[randomCall,allClass];
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    
    self.window.rootViewController = tabBarController;
    [self.window makeKeyAndVisible];


    return YES;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值