Sergey Demchenko Sergey Demchenko - 5 months ago 24
iOS Question

viewWillAppear is not called when add child controller to table view cell

When I add child view controller to table view cell, it looks like viewWillAppear for child view controller is not called, only viewDidAppear.

Table View Controller method:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("ShopInfoTableViewCell", forIndexPath: indexPath) as! ShopInfoTableViewCell
self.addChildViewController(self.shopInfoViewController, toView: cell.containerView)
return cell
}


View Controller category method:

- (void)addChildViewController:(UIViewController *)childController toView:(UIView *)view
{
[self addChildViewController:childController];
[view addSubview:childController.view];
[childController didMoveToParentViewController:self];

[childController.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(view.mas_top);
make.bottom.equalTo(view.mas_bottom);
make.left.equalTo(view.mas_left);
make.right.equalTo(view.mas_right);
}];
}


Any ideas why it happen?

Answer
- (void)addChildViewController:(UIViewController *)childController toView:(UIView *)view
{
    [self addChildViewController:childController];

    //add this
    [childController beginAppearanceTransition:YES animated:YES];
    [view addSubview:childController.view];
    [childController endAppearanceTransition];

    [childController didMoveToParentViewController:self];

    [childController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top);
        make.bottom.equalTo(view.mas_bottom);
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
    }];
}

Likewise, you should call this when you want to disappear

    [childController beginAppearanceTransition:NO animated:YES];
    [childController.view removeFromSuperview];
    [childController endAppearanceTransition];