【问题标题】:Enlarge UIScrollview's content size dynamically when adding views programatically以编程方式添加视图时动态放大 UI Scrollview 内容大小
【发布时间】:2015-12-17 10:34:36
【问题描述】:

我有以下情况:

  • 带有一个孩子的 UIScrollView ("_scrollView"):UIView
  • UIView ("_layoutContainer") 的子视图数量未知
  • UIView 的子视图以编程方式添加,其约束以编程方式设置。

添加子视图的代码(以及我尝试调整滚动视图的内容大小):

- (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];
}

我的情节提要约束如下所示:

此代码在日志中没有任何约束错误,并且看起来像预期的那样工作。

我的问题是: 滚动视图的内容大小不会改变。我可以添加任意数量的标签,但滚动永远不起作用。 你能帮我动态放大我的滚动视图吗?

【问题讨论】:

    标签: ios objective-c uiscrollview autolayout nslayoutconstraint


    【解决方案1】:

    如果所有约束都设置为在容器视图中定义一个内在的内容大小,滚动视图-contentSize 应该相应地调整大小,只需在添加内容后在滚动视图上调用-(void)invalidateIntrinsicContentSize

    - (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];
    }
    

    【讨论】:

    • 不起作用。如果我在添加所有视图后记录 _layoutContainer 大小,我仍然会得到 {{0, 0}, {1024, 768}} 。我认为滚动视图中的视图没有更新。将测试更多..
    • 好的,也可以试试:不要在循环中调用-layoutIfNeeded,在循环之后对容器视图说-setNeedsLayout,然后是-layoutIfNeeded。您应该始终将视图标记为需要布局,否则布局引擎将跳过它。我更新了答案
    • 做到了(并更新了我问题中的代码)。不幸的是,它没有改变任何东西。
    • 知道了!我必须为最后一个标签(循环之后)添加底部视图约束:if (lastLabel) { [_layoutContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousLabel]-(10)-|" options:0 metrics:nil views:@{@"previousLabel":lastLabel}]]; } 。请编辑您的答案,以便我接受。您的 如果所有约束都构成了足够的集合 (...) 为我指明了正确的方向
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多