I've been searching high and low to try and figure this out hopefully someone can help (I think this is probably fairly trivial but I'm just over confusing the situation!)
I'm using XCode 5 and storyboards. I have a container view which takes up around 75% of the screen space on an iPhone. Above this I have some buttons (and other widgets). I want to be able to load in a view controller into the container when pushing one of the buttons.
So far all of this works ok, but the view controllers that are being loaded in are of different sizes, and could change in future. I would like to be able to allow the child view (of arbitrary size) to scroll.
I've tried embedding the container in a scroll view, but then as part of the process of add the new child view, you have to set the frame size, but I want the child to figure out how big it should be and then set everything accordingly.
I'm not entirely sure what the best way to go about this is (either setting constraints in the story board, or programmatically?)
Any advice would be great! Or a link to a tutorial would be very useful, the only ones I could find were for adding view controllers to a container view or adding views to a scroll view, none of which seem to work quite as expected with Xcode 5 (iOS 7)
Thanks!
-Edit-
Here's the code I used to present a detail view controller of arbitrary size, the rest was all set in the storyboard (just in case this is useful to anyone in future!) This now works:
- (void)presentDetailController:(UIViewController*)detailVC{
//0. Remove the current Detail View Controller shown
if(self.currentDetailViewController){
[self removeCurrentDetailViewController];
}
//1. Add the detail controller as child of the container
[self addChildViewController:detailVC];
//2. DO NOT Define the detail controller's view size
//detailVC.view.frame = [self frameForDetailController];
//3. Add the Detail controller's view to the Container's detail view and save a reference to the detail View Controller
[self.detailView addSubview:detailVC.view];
self.currentDetailViewController = detailVC;
// Pin the height to the container to make it scrollable?
[self.detailView addConstraint:[NSLayoutConstraint constraintWithItem:self.detailView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant:detailVC.view.bounds.size.height]];
//4. Complete the add flow calling the function didMoveToParentViewController
[detailVC didMoveToParentViewController:self];
}
You can set constraints in either place and have it work correctly. The secret is this: you must set your container view's constraints to the top, left, bottom, and right of the scroll view that contains it. Autolayout will then calculate the scroll view's content size correctly based on what is inside of the container view.
See the section titled Pure Auto Layout Approach in the iOS 6 release notes. There's even some example code.