mardi 5 juillet 2016

UICollectionViewCell Auto Layout Programmatically

I am trying to create a UICollectionViewCell subclass with programmatically. I am trying to create with auto layout support.

I want to a UIView to the content view of the cell with adding leading, top, trailing and bottom constraints. But When I do that, My UIView's width and height stays zero. Here is my code inside initWithFrame method:

    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    _someView = [[UIView alloc] initWithFrame:CGRectZero];
    _someView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:_someView];
    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-2-[someView]-2-|" options:0 metrics:nil views:@{@"someView" : _someView}];
    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-2-[someView]-2-|" options:0 metrics:nil views:@{@"someView" : _someView}];

    [self.contentView addConstraints:horizontalConstraints];
    [self.contentView addConstraints:verticalConstraints];

And here is the UI debugger's screenshot: enter image description here

Why are these constraints are greyed out?

Thank you!

Aucun commentaire:

Enregistrer un commentaire