Search code examples
iosobjective-cuibuttonchildviewcontroller

Adding a ChildViewController causes strange behavior to the child viewcontroller


By "strange", I am noticing the following behavior:

  • background color set by the ChildViewController does not appear
  • despite adding a basic tap gesture recognizer to the ChildViewController, there is no tap recognition
  • adding a UIButton to the ChildViewController results in the UIButton shown, but there is no response when tapping on the UIButton part

In my ParentViewController, I present the ChildViewController very generically, like so:

UIViewController *viewController;
viewController = (ChildViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];

[self addChildViewController:viewController];

viewController.view.frame = self.view.bounds;
viewController.view.translatesAutoresizingMaskIntoConstraints = NO;
viewController.view.center = self.view.center;
[self.view addSubview:viewController.view];
[self.view bringSubviewToFront:viewController.view];

[viewController didMoveToParentViewController:self];

And here are some very basic things I do in ChildViewController's viewDidLoad:

self.view.backgroundColor = [UIColor yellowColor];
UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeSystem];
[tapButton addTarget:self action:@selector(tappedOnTap) forControlEvents:UIControlEventTouchUpInside];
//button view customization code omitted for brevity 
tapButton.userInteractionEnabled = YES;
tapButton.enabled = YES;
[self.view addSubview:tapButton];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOnView)];
[self.view addGestureRecognizer:tapGesture];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:tapGesture];

I ensured that everything is hooked up correctly - however, there is no acknowledgment in tappedOnView and tappedOnTap when I tap on it, in both simulator or device.

Am I missing something basic about presenting a ChildViewController?

Edit

For those curious, this very basic app is on Github.


Solution

  • The containment calls are fine. But if you use the view debugger (view debugger), you'll see the frame is not correct. This is because you've turned off translatesAutoresizingMaskIntoConstraints, but you don't have any constraints. You can either turn that back on (and set autoresizingMask accordingly):

    viewController.view.translatesAutoresizingMaskIntoConstraints = true;
    

    Or you can define constraints rather than setting the frame (and redundantly setting the center):

    //viewController.view.frame = self.view.bounds;
    viewController.view.translatesAutoresizingMaskIntoConstraints = false;
    //viewController.view.center = self.view.center;
    
    [self.view addSubview:viewController.view];
    
    [NSLayoutConstraint activateConstraints:@[
        [viewController.view.topAnchor constraintEqualToAnchor:self.view.topAnchor],
        [viewController.view.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
        [viewController.view.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [viewController.view.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
    ]];