1

I have this views hierarchy in a xib file:

  • UIView
  • UIScrollView
    • UIView
      • UIView
      • UITableView
  • UIButton

Let's call contentView the UIView that is the direct child of the UIScrollView. I've set its top, bottom, leading and trailing constraints to pin the scroll view. Then, since I'm populating the table view at runtime and I don't know its height beforehand, I set the scroll view's contentSize in code:

[self.scrollView setContentSize:CGSizeMake(self.contentView.frame.size.width, self.tableView.frame.size.height)];

But I don't make this work... what could I be missing?

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
AppsDev
  • 12,319
  • 23
  • 93
  • 186

5 Answers5

2

AppsDev, check this video out it helped me a lot doing UIScrollView via storyboard https://www.youtube.com/watch?v=UnQsFlMGDsI

Also never set the scrollView's contentSize as it has to be determined by scrollView on its own and that's why we have AutoLayout.

I believe we should have one tag for uiscrollview-autolayout

Sana
  • 9,895
  • 15
  • 59
  • 87
0

Don't set the content size manually. Instead,

  1. Constrain your contentView's four edges to the edges of the scrollView.
  2. Constrain your contentView's width to be equal to the scrollView's width. (This will prevent the content from being wider than the scrollView.)
  3. Constrain the contentView's top and sides to the corresponding edges of the child view.
  4. Constrain the contentView's sides and bottom to the sides and bottom of the tableView.
  5. Now here's where it gets tricky: constrain the bottom of the child view to be equal to the top of the table view. However, unless you explicitly set a height constraint on the child view, you'll get a layout error that the height of the scrollView's contents will be ambiguous. To get around this, you can set the child's placeholder height to make Interface Builder happy, but then you'll also have to set its height somewhere at runtime.

Now you should be set. The scrollView can now calculate the full height and width of its contents by examining the constraint hierarchy, and you don't have to set its content height manually.

NRitH
  • 13,441
  • 4
  • 41
  • 44
0

I finally managed to make this work by following the @Sana answer and also this post to be able to scroll the table view content.

Thanks u all for replying.

Community
  • 1
  • 1
AppsDev
  • 12,319
  • 23
  • 93
  • 186
-1

Just for example:

UIView *containerView               = [[UIView alloc] init];
    UIScrollView *scrollView            = [[UIScrollView alloc] init];
    [containerView addSubview:scrollView];

    containerView.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.translatesAutoresizingMaskIntoConstraints    = NO;
    NSDictionary *viewsDictionary       = NSDictionaryOfVariableBindings(containerView, scrollView);

    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|"
                                                                          options:kNilOptions
                                                                          metrics:nil
                                                                            views:viewsDictionary]];
    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
                                                                          options:kNilOptions
                                                                          metrics:nil
                                                            views:viewsDictionary]];
Tanuj
  • 531
  • 4
  • 10
-1

Use this code My code help you.

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
   [self.scrollView setContentSize:CGSizeMake(self.contentView.frame.size.width, self.tableView.frame.size.height)];
}
Vidhi Patel
  • 603
  • 5
  • 10