0

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?

Burak
  • 5,706
  • 20
  • 70
  • 110
  • What do you mean it goes off? You should set both left, top, right and bottom constraints. – mattsson Jan 12 '17 at 14:16
  • I add the other constraints but still cannot see the view. – Burak Jan 12 '17 at 14:21
  • 1
    Have you specified the width of the UIScrollView using AutoLayout? Can I see the code for laying out the UIScrollView? – mattsson Jan 12 '17 at 14:28
  • From the constraints you specified, your cell shouldn't be hidden. It should instead occupy the `entire superview` i.e, `self.horizontalScrollView` – KrishnaCA Jan 12 '17 at 19:34

2 Answers2

0

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 :)

KrishnaCA
  • 5,615
  • 1
  • 21
  • 31
0

Quicker option:

[cell mas_updateConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(@0);
}];
szubiszon
  • 106
  • 1
  • 7