简体   繁体   中英

Enlarge UIScrollview's content size dynamically when adding views programatically

I have the following situation:

  • UIScrollView ("_scrollView") with one child: UIView
  • UIView ("_layoutContainer") has a unknown number of childViews
  • childViews of UIView are added programmatically and their constraints are set programmatically.

Code for adding childviews (and my try to resize scrollview's content size):

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView* lastLabel = _topCollectionView;
    for (int i = 0; i<50; i++) {
        UILabel* imageLabelView = [UILabel new];
        imageLabelView.translatesAutoresizingMaskIntoConstraints = NO;

        [_layoutContainer addSubview:imageLabelView];

        [_layoutContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[labelview]-(10)-|" options:0 metrics:nil views:@{@"labelview":imageLabelView}]];
        [_layoutContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousLabel]-(10)-[imagelabel(40)]" options:0 metrics:nil views:@{@"previousLabel":lastLabel, @"imagelabel":imageLabelView}]];

        imageLabelView.text = @"DUMMY";
        lastLabel = imageLabelView;
    }

    [_layoutContainer setNeedsLayout];
    [_layoutContainer layoutIfNeeded];
    _scrollView.contentSize = _layoutContainer.frame.size;
    [_scrollView invalidateIntrinsicContentSize];
}

My Storyboard Constraints look like this:

情节提要中的约束

This code works without any constraint errors in log and it looks like expected.

My problem is: The scrollview's content size doesn't change. I can add as many labels as I want to, but scrolling never works. Can you please help me to dynamically enlarge my scrollview?

If all the constraints makes a sufficient set to define an intrinsic content size in the container view, the scroll view -contentSize should resize accordingly, just need to call -(void)invalidateIntrinsicContentSize on the scroll view after you added the content.

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView* lastLabel = _topCollectionView;
    for (int i = 0; i<50; i++) {
        UILabel* imageLabelView = [UILabel new];
        imageLabelView.translatesAutoresizingMaskIntoConstraints = NO;

        [_layoutContainer addSubview:imageLabelView];

        [_layoutContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(10)-[labelview]-(10)-|" options:0 metrics:nil views:@{@"labelview":imageLabelView}]];
        [_layoutContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousLabel]-(10)-[imagelabel(40)]" options:0 metrics:nil views:@{@"previousLabel":lastLabel, @"imagelabel":imageLabelView}]];

        imageLabelView.text = @"DUMMY";
        lastLabel = imageLabelView;
    }

    [_layoutContainer setNeedsLayout];
    [_layoutContainer layoutIfNeeded];
    [_scrollView invalidateIntrinsicContentSize];
    [_scrollView setNeedsLayout];
    [_scrollView layoutIfNeeded];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM