0

I have a UIViewController (added to a UINavigationController) that adds another UIViewController's view using standard code:

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

That's working fine.

The childViewController's (toVC) view is constructed using Interface Builder and Auto Layout, and it contains a UIScrollView. If I load toVC into my app directly into a UINavigationController (instead of adding it to another view controller) scrolling works perfectly.

However, when adding toVC to my mainVC using the above code, toVC's scrollView doesn't scroll at all and I'm at a loss as to what I need to do.

djibouti33
  • 12,102
  • 9
  • 83
  • 116
  • Is it necessary for you to add it as a subview? Does it work if you present the viewController? – Logan Mar 03 '14 at 19:34
  • If from my masterVC I issue `[self presentViewController:toVC ...]` the scrollView still does not scroll. – djibouti33 Mar 03 '14 at 19:39
  • http://stackoverflow.com/questions/2824435/uiscrollview-not-scrolling -- Did you try these solutions? – Logan Mar 03 '14 at 19:43
  • yeah, like i mentioned, if I just load it directly into a UINavigationController, it works fine, so I feel good about the constraints I've set up. It's only when I add the UIScrollView to another UIViewController's view when the scrolling stops working. – djibouti33 Mar 03 '14 at 19:52
  • I had a similar problem a couple weeks ago, but I don't remember how I fixed it. I remember it was something strange, I'll see if I can figure it out. Sorry I couldn't help. – Logan Mar 03 '14 at 19:56
  • haha, @logan, definitely let me know if you can remember :) – djibouti33 Mar 03 '14 at 19:57

1 Answers1

1

You should set the frame and the autoresizing mask / auto layout constraints of the child view controller's view. Even though the child controller's view is set up with auto layout, it still needs to be positioned in the parent view controller's view with whatever system the parent controller is using.

EDIT: Another potential issue, since you mentioned that the scroll view doesn't scroll when presented:

Did you set top, left, right, and bottom constraints for the scroll view's subviews? Scroll views treat "space to superview" constraints differently than normal views do; rather than defining where its subview is positioned, these constraints define the content size. Failing to set constraints on both top and bottom, or both left and right, may leave the scroll view with a content size of {0, 0}, in which case the scroll view would not scroll.

Austin
  • 5,625
  • 1
  • 29
  • 43
  • Can you explain what you meant with "Even though the child controller's view is set up with auto layout, it still needs to be positioned in the parent view controller's view"? I am having the same problem. I have two childVCs and i am not sure how to set them up in the parentVC's scrollview. People say we should not set frames and contentsizes manually if we are using Autolayout. So how will i manage what you said above? – aytunch Nov 08 '15 at 14:57
  • @aytunch What I meant is that the child view controller's subviews are managed with auto layout, but that its view's relationship to the parent view controller's view still needs to be defined. – Austin Nov 10 '15 at 14:44
  • controller.view.translatesAutoresizingMaskIntoConstraints = false saved me a lot of time – Ronaldo Albertini Apr 20 '17 at 14:01