How to place a child view inside a scroll view with Masonry?

174 Views Asked by At

I want to place a view into a 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];

Price cell can be seen now. But if I add this code:

[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);
}];

price view is hidden. Where am I wrong?

2

There are 2 best solutions below

0
On BEST ANSWER

The problem in the following block of code are the top, left, bottom and right constraints.

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

Here, you are making your top constraint equal to 0, i.e, the top border of your cell is 0. Similarly, the left, right and bottom borders of your view are 0. You only added width and height of your cell.

From the constraints you've given here, the iOS auto-layout system gets the frame of cell as cell.superview.bounds. From this, your cell shouldn't be hidden. It should instead occupy the entire view.

Feel free to suggest edits to make this better. Feel free to comment if anything is unclear or you're getting a different result :)

0
On

Quicker option:

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