A UIView has a SizeToFit method that will make the UIView fit all of it's subviews. Is there anything like that, which will just return the size that it calculates and not modify any view's frame.
I have several subviews on a UIScrollView and I want to do SizeToFit on the scroll view's contentSize, rather than it's frame. I have to do it on the contentSize, because I don't want to increase the "real" size of the UIScrollView, and the content is loaded dynamically and asynchronously so I can't do it manually when I add the subviews to the UIScrollView.
At the moment, this is the best I have:
CGRect contentRect = CGRectZero;
for (UIView *view in self.subviews)
contentRect = CGRectUnion(contentRect, view.frame);
self.contentSize = contentRect.size;
If your function is called frequently, you wouldn't want to iterate over the subviews each time. Instead, whenever a subview is added, do a union of the current contentSize and the frame of the new subview.
- (void)didAddSubview:(UIView *)subview {
CGRect tmp;
tmp.origin = CGPointZero;
tmp.size = self.contentSize;
tmp = CGRectUnion(tmp,subview.frame);
self.contentSize = tmp.size;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With