ios 关于UITabBarController的自定义

让类继承于UITabBarController,在delegate中添加

//1.创建Window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    mainViewController *mainCtr = [[mainViewController alloc] init];
    
    self.window.rootViewController = mainCtr;
    
    [self.window makeKeyAndVisible];
    
    return YES;

然后在mainViewController.m添加

@interface mainViewController ()
{
    UIButton *btn;
    NSInteger buttonNum;
}

@property (nonatomic, weak) UIButton *selectedBtn;

@end

@implementation mainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //需要定义的选项卡的数量
    buttonNum = 4;
    
    //删除现有的tabBar
    CGRect rect = self.tabBar.frame;
    [self.tabBar removeFromSuperview];  //移除TabBarController自带的下部的条
    
    SYViewController *SYCtr = [[SYViewController alloc] init];
    SYCtr.tabBarHeigt = rect.size.height;
    RWViewController *RWCtr = [[RWViewController alloc] init];
    JLViewController *JLCtr = [[JLViewController alloc] init];
    MeViewController *MeCtr = [[MeViewController alloc] init];
    MeCtr.tabBarHeigt = rect.size.height;
    NSLog(@"%f",rect.size.height);
    self.viewControllers = @[SYCtr,RWCtr,JLCtr,MeCtr];
    
    //测试添加自己的视图
    UIView *myView = [[UIView alloc] init];
    myView.frame = rect;
    [self.view addSubview:myView];
    
    NSArray *array = [NSArray arrayWithObjects:@"首页",@"任务",@"奖励",@"我的", nil];
    
    for (int i = 0; i < buttonNum; i++) {
        UIView *view = [[UIView alloc] init];
        
        CGFloat x = i * myView.frame.size.width / buttonNum;
        view.frame = CGRectMake(x, 0, myView.frame.size.width / buttonNum, myView.frame.size.height);
        view.backgroundColor = [UIColor blueColor];
        
        [myView addSubview:view];
        
        btn = [[UIButton alloc] initWithFrame:CGRectMake(view.frame.size.width / 6, 5, 2*view.frame.size.width / 3, view.frame.size.height - 10)];
        [btn setTitle:array[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.layer.cornerRadius = 8;
        [btn setBackgroundColor:[UIColor orangeColor]];
        btn.tag = i;//设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图
    
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [view addSubview:btn];
        
        //设置刚进入时,第一个按钮为选中状态
        if (i == 0) {
            btn.selected = YES;
            self.selectedBtn = btn;  //设置该按钮为选中的按钮
            self.selectedIndex = i;
        }
    }
}
- (void)clickBtn:(UIButton *)button {
    //1.先将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    NSLog(@"%d",self.selectedBtn.tag);

    //2.再将当前按钮设置为选中
    button.selected = YES;
    //3.最后把当前按钮赋值为之前选中的按钮
    self.selectedBtn = button;

    //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)
    self.selectedIndex = button.tag;
}

这样简单的关于UITabBarController的自定义便完成了。
效果图如下:

ios 关于UITabBarController的自定义_第1张图片

你可能感兴趣的:(ios 关于UITabBarController的自定义)