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的自定义便完成了。
效果图如下:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,359评论 25 708
  • 关于运营案例系列:我们日常经常接触很多优秀的运营案例,通过剖析和复盘这些项目所运用的运营技巧及背后的运营思维,可以...
    瓦力求索阅读 1,820评论 1 29
  • 每一个人, 有缘与你相会。 每一件事, 有缘与你遇见。 每一种境界, 有缘与你同在。 你不再是单独的一个人, 与大...
    小剧在成长阅读 486评论 2 4
  • 七绝 微波炉 棱角分明笃意深,微波屡屡热温侵。 无非弹指须臾过,辘辘饥肠美味临。 ~老财神 2017年11月05日
    老财神阅读 531评论 0 4
  • 因为一颗流浪的心, 仍在找寻一个归属! 因为向往外面的世界, 依然踏上一段未知的旅途! 因为期一场不期而遇邂逅, ...
    小啦苏阅读 154评论 0 1