纯代码构建tabbar

纯代码构建tabbar,构建完成之后在UITabBar的代理里面添加相应的事件
 
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, SCREENHEIGHT - 50, SCREENHWIDTH, 50)];
    tabBar.delegate = self;
    [self.view addSubview:tabBar];
    
    UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"ico03"] selectedImage:[UIImage imageNamed:@"saoma"]];
    item1.tag = 101;
    
    UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"商城" image:[UIImage imageNamed:@"ico03"] selectedImage:[UIImage imageNamed:@"saoma"]];
    item2.tag = 102;
    
    UITabBarItem *item3 = [[UITabBarItem alloc]initWithTitle:@"订单" image:[UIImage imageNamed:@"ico03"] selectedImage:[UIImage imageNamed:@"saoma"]];
    item3.tag = 103;
    
    UITabBarItem *item4 = [[UITabBarItem alloc]initWithTitle:@"服务单" image:[UIImage imageNamed:@"ico03"] selectedImage:[UIImage imageNamed:@"saoma"]];
    item4.tag = 104;
    
    UITabBarItem *item5 = [[UITabBarItem alloc]initWithTitle:@"我" image:[UIImage imageNamed:@"ico03"] selectedImage:[UIImage imageNamed:@"saoma"]];
    item5.tag = 105;
    
    tabBar.items = @[item1, item2, item3, item4, item5];
    
    tabBar.selectedItem = item3;
    [self orderPage];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger tg = item.tag;
    
    if (tg == 101) {
        LILog(@"101");
        [self.navigationController popViewControllerAnimated:NO];
    } else if (tg == 102) {
        LILog(@"102");
        [self ecShopPage];
    } else if (tg == 103) {
        LILog(@"103");
        [self orderPage];
    } else if (tg == 104) {
        LILog(@"104");
        [self servicePage];
    } else if (tg == 105) {
        LILog(@"105");
        [self profilePage];
    }
}




你可能感兴趣的:(ios)