daspianist - 10 months ago 40
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
ChildViewController
does not appear

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

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"];

viewController.view.frame = self.view.bounds;
viewController.view.center = self.view.center;
[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];
//button view customization code omitted for brevity
tapButton.userInteractionEnabled = YES;
tapButton.enabled = YES;

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


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.

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;