0

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:

enter image description here

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

Community
  • 1
  • 1
LS_
  • 6,763
  • 9
  • 52
  • 88
  • can you print out the values of contentRect and _contentView.frame in the recalculate method ? – Teja Nandamuri May 11 '16 at 13:42
  • @TejaNandamuri I've edited the question with the NSLog – LS_ May 11 '16 at 13:52
  • 1
    Instead of doing it in `viewDidLoad`, what happens if you do it in `viewDidAppear`? – trojanfoe May 11 '16 at 14:20
  • @trojanfoe DAMNNNN I tried to move in the viewDidAppear only the method that recalculated the height and it did not work, now I moved everything inside it and it works fine, thanks a lot you saved me hours :S – LS_ May 11 '16 at 14:27
  • @trojanfoe if you want post it as an answer so I can accept! :) – LS_ May 11 '16 at 14:41
  • @Signo It's a pretty common problem so I've marked the question as a dupe of one of them. – trojanfoe May 11 '16 at 14:51
  • @trojanfoe Ok, thanks! I'm sorry for the duplicate but I wasn't able to find the existing question – LS_ May 11 '16 at 15:00

1 Answers1

1

I've seen cases where a UIScrollView will try and be helpful by offsetting itself in order to incorporate the navigation bar even if it's hidden. Have you tried disabling this property?

self.automaticallyAdjustsScrollViewInsets

I assume you have scrollEnabled set to YES? This is a bit of a shot in the dark, but I noticed others resolving similar issues here by disabling and re-enabling scrolling:

Large Text Being Cut Off in UITextView That is Inside UIScrollView

Community
  • 1
  • 1
Brian Bethke
  • 154
  • 6