Shouldn't there be a way to resize the frame of a UIView after you've added subviews so that the frame is the size needed to enclose all the subviews? If your subviews are added dynamically how can you determine what size the frame of the container view needs to be? This doesn't work:
[myView sizeToFit];
Whatever module that dynamically added all these subviews had to know where to put them (so they relate properly, or so they don't overlap, etc.) Once you know that, plus the size of the current view, plus the size of the subview, you have all you need to determine if the enclosing view needs to be modified.
You could also add the following code to calculate subviews position.
UIViewUtils.h
UIViewUtils.m
Old question but you could also do this with a recursive function.
You might want a solution that always works no matter how many subviews and subsubviews,...
Update : Previous piece of code only had a getter function, now also a setter.
Here is a swift version of the accepted answer, also small change, instead of extending it this method gets the view as a variable and returns it.
Heres the extending version:
Should work, why don't you check the CGRect before and after?
When you make your own UIView class, consider overriding IntrinsicContentSize in the subclass. This property is called by OS to know the recommended size of your view. I will put code snippet for C# (Xamarin), but the idea is the same:
This returned size depends completely on your design. For example, if you just added a label, then width and height is just the width and height of that label. If you have more complex view, you need to return the size that fits all your subviews.