【问题标题】:Why doesn't contentView containing a label have any height in its scrollView in autolayout?为什么包含标签的 contentView 在自动布局中的滚动视图中没有任何高度?
【发布时间】:2015-01-07 00:04:35
【问题描述】:

我正在创建一个滚动视图并使用自动布局向它添加一个内容视图。我认为 contentView 的高度将取决于其内容的高度和约束。既然有标签,那么contentView的高度不应该等于标签的高度吗?唯一存在高度的方法(快照中的蓝色)是向 contentView 添加高度约束。我认为这是不必要的,因为自动布局应该从其固有内容大小中知道标签的高度?

.

// Scrollview

_scrollView = [UIScrollView new];
_scrollView.translatesAutoresizingMaskIntoConstraints = NO;
_scrollView.backgroundColor = [UIColor yellowColor];
_scrollView.bounces = YES;
[self.view addSubview:_scrollView];

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:0 views:@{ @"_scrollView" : _scrollView }];
NSArray *verticalConstraints   = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:0 views:@{ @"_scrollView" : _scrollView }];
[self.view addConstraints:horizontalConstraints];
[self.view addConstraints:verticalConstraints];


// Content view

_contentView = [UIView new];
_contentView.translatesAutoresizingMaskIntoConstraints = NO;
_contentView.backgroundColor = [UIColor blueColor];
[_scrollView addSubview:_contentView];

NSArray *horizontalContentConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_contentView]|" options:0 metrics:0 views:@{ @"_contentView" : _contentView }];
NSArray *verticalContentConstraints   = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_contentView]|" options:0 metrics:0 views:@{ @"_contentView" : _contentView }];
[_scrollView addConstraints:horizontalContentConstraints];
[_scrollView addConstraints:verticalContentConstraints];

// Why does height of content view need to be added to get the blue area to show?
NSLayoutConstraint *contentViewHeight = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:NSLayoutAttributeNotAnAttribute attribute:NSLayoutAttributeHeight multiplier:1 constant:5];
[_contentView addConstraint:contentViewHeight];

NSLayoutConstraint *contentViewOuterLeading = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
NSLayoutConstraint *contentViewOuterTrailing = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];

[self.view addConstraints:@[ contentViewOuterLeading, contentViewOuterTrailing ]];


  // Day label

_dayLabel = [UILabel new];
_dayLabel.translatesAutoresizingMaskIntoConstraints = NO;
_dayLabel.text = @"Monday";
_dayLabel.backgroundColor = [UIColor greenColor];
[_contentView addSubview:_dayLabel];

NSLayoutConstraint *dayLabelTop = [NSLayoutConstraint constraintWithItem:_dayLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];

NSLayoutConstraint *dayLabelCenterX = [NSLayoutConstraint constraintWithItem:_dayLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];

[_contentView addConstraints: @[ dayLabelTop, dayLabelCenterX ]];

【问题讨论】:

    标签: objective-c uiscrollview autolayout


    【解决方案1】:

    这是因为 UIScrollView 的内容大小为 (0,0)。

    你可以说它的边界应该与超级视图齐平,但这实际上并没有改变它的 contentSize。此外,您添加了 contentView,但您再次说它应该与其父视图(滚动视图)齐平,尽管由于滚动视图的大小仍然是(0,0),您隐含地说内容的视图大小将为0,0。只有在您添加一个明确表示为 5px 的约束之后,您才能真正看到一些视图。

    滚动视图会从它的内容中得到合适的大小,所以如果你将内容视图实例化为:

    UIView * contentView = [[UIView alloc] initWithFrame:[UIScreen mainscreen].bounds]; 
    

    滚动视图的内容大小将等于 [UIScreen mainscreen].bounds 并且您仍将满足您编写的布局约束。

    当您将 UI 元素添加到 uiscrollview 并通过布局约束排列它们时,滚动视图将根据需要自动展开。

    我写了一篇关于这个主题的博客文章,Programmatic ScrollViews w/ NSLayout ...你可能也看过一个较旧的提交,但我遇到了这个完全相同的问题。

    【讨论】:

    • 有趣。为此,我需要知道所有组件的大小,将它们相加并计算出一个大小。我正在尝试像spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios 这样的纯自动布局方法。它依赖于内在的内容大小来获取 contentView 的高度,通过其他约束传递给 scrollView。
    • 是的,您可能需要提前知道它们的大小,或者您可以在运行时计算。理论上,如果您正在加载创建 VFL 为 H:|[image1][image2][image3]| 的图像,它可以自行处理。链接也不错!我也写了类似的东西(但以编程方式),但是完美地完成这一切真是太痛苦了。
    【解决方案2】:

    好的,想通了。将 contentView 的约束添加到其标签的底部使其工作。 contentView 不需要高度限制。这是更新的代码:

    // Scrollview
    
    _scrollView = [UIScrollView new];
    _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    _scrollView.backgroundColor = [UIColor yellowColor];
    _scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:_scrollView];
    
    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:0 views:@{ @"_scrollView" : _scrollView }];
    NSArray *verticalConstraints   = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:0 views:@{ @"_scrollView" : _scrollView }];
    [self.view addConstraints:horizontalConstraints];
    [self.view addConstraints:verticalConstraints];
    
    
    // Content view
    
    _contentView = [UIView new];
    _contentView.translatesAutoresizingMaskIntoConstraints = NO;
    _contentView.backgroundColor = [UIColor blueColor];
    [_scrollView addSubview:_contentView];
    
    NSArray *horizontalContentConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_contentView]|" options:0 metrics:0 views:@{ @"_contentView" : _contentView }];
    NSArray *verticalContentConstraints   = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_contentView]|" options:0 metrics:0 views:@{ @"_contentView" : _contentView }];
    [_scrollView addConstraints:horizontalContentConstraints];
    [_scrollView addConstraints:verticalContentConstraints];
    
    NSLayoutConstraint *contentViewOuterLeading = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
    NSLayoutConstraint *contentViewOuterTrailing = [NSLayoutConstraint constraintWithItem:_contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
    [self.view addConstraints:@[ contentViewOuterLeading, contentViewOuterTrailing ]];
    
    
    // Day label
    
    _dayLabel = [UILabel new];
    _dayLabel.translatesAutoresizingMaskIntoConstraints = NO;
    _dayLabel.text = @"Monday";
    _dayLabel.backgroundColor = [UIColor greenColor];
    [_contentView addSubview:_dayLabel];
    
    NSLayoutConstraint *dayLabelTop = [NSLayoutConstraint constraintWithItem:_dayLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    NSLayoutConstraint *dayLabelCenterX = [NSLayoutConstraint constraintWithItem:_dayLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *dayLabelBottom = [NSLayoutConstraint constraintWithItem:_dayLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
    [_contentView addConstraints: @[ dayLabelTop, dayLabelCenterX, dayLabelBottom ]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多