UIScrollView 子控件的自动布局经验

按照正常的自动布局方式给UIScrollview进行布局的话,子控件不显示,这是因为ScrollView的content size进行自动布局的话需要通过子控件进行约束,则就需要处理好子控件的布局

需要注意以下两点:

1.子空间根据scrollview的父容器进行约束

2,固定好子空间的宽高

demo:

_scrollView = [[UIScrollView alloc] init];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.delegate = self;
    _scrollView.scrollsToTop = NO;
//    _scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    [self.imagesView addSubview:_scrollView];
    [_scrollView autoPinEdgesToSuperviewEdges];
    
    _tableView = [[ZKTableView alloc] init];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollsToTop = NO;
    if(IS_IphoneX) {
        [_tableView setContentInset:UIEdgeInsetsMake(0, 0, 44 + 20, 0)];
    } else {
        [_tableView setContentInset:UIEdgeInsetsMake(0, 0, 20, 0)];
    }
    [self.scrollView addSubview:_tableView];
    [_tableView autoPinEdgesToSuperviewEdges];
    [_tableView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.imagesView];
    [_tableView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.imagesView];

原文地址:https://www.cnblogs.com/zrr-notes/p/10900144.html