[buttonCollectionView addConstraint:[NSLayoutConstraint constraintWithItem:maskView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:buttonCollectionView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:maskHeight]];
我创建了向UICollectionView
添加视图,但为什么它的位置错误?
像这样:UIView: 0x188bbee0; frame = (0 0; 100 5)
;
CollectionView's frame is: UICollectionView: 0x178b8e00; frame = (0 0; 320 44);
编辑:
添加其他约束
maskView = [[UIView alloc] init];
maskView.backgroundColor = [UIColor colorWithRed:137 / 255.0f green:209 / 255.0f blue:198 / 255.0f alpha:1];
[maskView setTranslatesAutoresizingMaskIntoConstraints:NO];
maskView.translatesAutoresizingMaskIntoConstraints = NO;
[buttonCollectionView addSubview:maskView];
CGFloat maskHeight = 3;
maskXPos = [NSLayoutConstraint constraintWithItem:maskView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:buttonCollectionView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:(CELL_WIDTH - MASK_VIEW_WIDTH) / 2];
[buttonCollectionView addConstraint:maskXPos];
[buttonCollectionView addConstraint:[NSLayoutConstraint constraintWithItem:maskView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:buttonCollectionView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:maskHeight]];
[maskView addConstraint:[NSLayoutConstraint constraintWithItem:maskView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:MASK_VIEW_WIDTH]];
[maskView addConstraint:[NSLayoutConstraint constraintWithItem:maskView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:maskHeight]];
答案 0 :(得分:0)
对底部尝试此约束:
[buttonCollectionView addConstraint:[NSLayoutConstraint constraintWithItem:buttonCollectionView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:maskView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];//For MaskView's bottom to be aligned with bottom of Collection view distance between two should be '0'
此外,如果你试图以X为中心,你可能会看到NSLayoutAttributeCenterX