1

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.

daspianist
  • 5,336
  • 8
  • 50
  • 94
  • maybe try moving it out of `viewDidLoad` to `viewDidAppear` and see what happens – random Sep 19 '16 at 21:18
  • strangely, moving it to `viewDidAppear` causes everything to disappear all together. – daspianist Sep 19 '16 at 21:21
  • try to remove this line [viewController didMoveToParentViewController:self]; and do this [self addChildViewController:viewController]; after [self.view addSubview:tapButton]; – Kuntal Gajjar Sep 19 '16 at 21:31
  • Thanks for the comment Kuntal. I commented out `didMoveToParentViewController:self`, but it didn't have any effect. Also, `[self addChildViewController:viewController` is called in the `ParentViewController`, while `[self.view addSubview:tapButton]` happens in `ChildViewController`, so I couldn't follow through on that suggestion. – daspianist Sep 19 '16 at 21:36
  • In your `viewDidLoad` (and `viewDidAppear`, etc.), are you calling the `super` implementations? – Rob Sep 19 '16 at 23:15
  • @Rob Yep definitely. `[super viewWillAppear: animated]`, etc – daspianist Sep 19 '16 at 23:17

2 Answers2

1

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]
]];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Wow, this simple thing did it. All of the previous issues have been solved as a result of setting this to `true`. Thanks for your insight on this, and for providing the code on setting the constraints. – daspianist Sep 20 '16 at 00:19
0

Try this:

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

viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleheight;

[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];

Usually you don't need to do anything when adding a ViewController to another except those basic methods. since the views will be adjusted automatically and you don't need to set all those properties like size and center etc...

Karim H
  • 1,543
  • 10
  • 24