daspianist daspianist - 1 year ago 66
iOS Question

Adding a ChildViewController causes strange behavior to the child viewcontroller

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

  • background color set by the
    does not appear

  • despite adding a basic tap gesture recognizer to the
    , there is no tap recognition

  • adding a
    to the ChildViewController results in the
    shown, but there is no response when tapping on the UIButton part

In my
, I present the
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

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
when I tap on it, in both simulator or device.

Am I missing something basic about presenting a


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

Rob Rob
Answer Source

The containment calls are fine. But if you use the 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],
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download