Inside the viewDidLoad
of my UIViewController
I create:
a UIScrollView
with the following code:
- (void)generateScrollView {
_scrollView = [[UIScrollView alloc] initWithFrame:_containerView.frame];
[_containerView addSubview:_scrollView];
[self generateContentView];
}
Where _containerView
is a UIView
created through the Interface Builder with the following constraints:
Then I generate another UIView
which i'll use as a container for all the UI Elements:
- (void)generateContentView {
_contentView = [[UIView alloc] initWithFrame:_scrollView.frame];
[_scrollView addSubview:_contentView];
}
Then I generate N UI Elements (UITextView
, UIImageView
ecc..) and finally I recalculate the height of both the contentView
and scrollView
with the following code:
- (void)recalculateScrollViewHeight {
CGRect contentRect = CGRectZero;
for (UIView *view in _contentView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
NSLog(@"ContentRect height: %f", contentRect.size.height);
}
_contentView.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y, _contentView.frame.size.width, contentRect.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height);
NSLog(@"_contentView height: %f", _contentView.frame.size.height);
_scrollView.contentSize = _contentView.frame.size;
}
The problem is that the _scrollView
cuts some of the content because it seems that the calculated height its not correct. I'm struggling with this problem and I can't seem to find a solution, does anyone know why the calculated height it's not correct? Am I missing a property for the UIScrollView
or UIView
?
EDIT:
This is the result of the 2 NSLog
:
ContentRect height: 180.000000
ContentRect height: 360.000000
ContentRect height: 540.000000
_contentView height: 604.000000
I've added 3 UIImageView
with an height of 180
so the result is correct, 540 + 20 (status bar height) + 44 (navigation bar height), but still the scrollView
cuts some pixels from the last image. Also there's no extra spacing between the images, they're one after another