0

I would like to add a custom UITabBar implementation to a UITableViewCell.

If a UITableViewCell was itself a UIViewController I could use view controller containment, but it isn't.

The best I can do right now is add the TabBar's view to the contentView of the cell in cellForRowAtIndexPath:

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    NSString *cellID;
    UITableViewCell *cell;

    cellID = @"TabCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellID] ?: [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    cell.contentView.backgroundColor = UIColor.blueColor;

    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    UIViewController *vc1 = [UIViewController new];
    vc1.view.backgroundColor = UIColor.magentaColor;
    vc1.title = @"TAB1";

    UIViewController *vc2 = [UIViewController new];
    vc2.view.backgroundColor = UIColor.purpleColor;
    vc2.title = @"TAB2";

    tabBarController.delegate = self;
    tabBarController.viewControllers = @[vc1, vc2];

    [cell.contentView addSubview:tabBarController.view];

    return cell
}

The problem is that the cell only shows the view of the TabBar and not the TabBar control itself.

How can I show the entire TabBar in a UITableViewCell?

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
kraftydevil
  • 5,144
  • 6
  • 43
  • 65

1 Answers1

0

Try using a Container View, which allows you to include a child View Controller. Here's another SO post with instructions on how to do that.

But I have to agree with what @rikkigibson said on the comments, perhaps you should reconsider your approach. Maybe we can help if you share more details on what you want to accomplish.

Community
  • 1
  • 1
André Fratelli
  • 5,920
  • 7
  • 46
  • 87