如何使用砌体将子视图放置在滚动视图中



我想将视图放入UIScrollView中。

Price* p = _entity.prices[0];
    ItemPriceCell *cell = [[ItemPriceCell alloc] initWithFrame:CGRectZero];
    cell.priceLabel.attributedText = [Helper stylePriceLabel:p.priceString withFont:[UIFont fontWithName:@"OpenSans-Semibold" size:34]];
    cell.priceLabel.textColor = [Helper color:self.shop.currentTheme.options.itemPriceTextColor];
    cell.priceNameLabel.text = [NSString stringWithFormat:@"%@",[p definition]];
    cell.priceNameLabel.textColor = [Helper color:self.shop.currentTheme.options.itemDetailTextColor];
    [self.horizontalScrollView addSubview:cell];

现在可以看到价格单元格。但是如果我添加此代码:

[cell  mas_updateConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(@200);
    make.height.equalTo(@50);
    make.top.equalTo(@0);
    make.left.equalTo(@0);
    make.right.equalTo(@0);
    make.bottom.equalTo(@0);
}];

价格视图处于隐藏状态。我错在哪里?

以下

代码块中的问题是topleftbottomright约束。

[cell mas_updateConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(@0);
     make.left.equalTo(@0);
     make.right.equalTo(@0);
     make.bottom.equalTo(@0);
}];

在这里,您将顶部约束设置为0,即单元格的上边框为0。同样,视图的左边框、右边框和下边框为 0。您只添加了单元格的widthheight

根据您在此处给出的约束,iOS 自动布局系统将单元格的框架获取为cell.superview.bounds .由此,您的单元格不应被隐藏。相反,它应该占据整个视图。

请随时建议编辑以使其更好。如果有任何不清楚的地方或您得到不同的结果,请随时发表评论:)

更快的选项:

[cell mas_updateConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(@0);
}];

最新更新