我需要使用 UICollectionView 的 anchor 约束来构建 UIView。我插入的 anchor 都是正确的,但我对 widthAncor 有问题......我解释一下:
这是我的 CollectionView
_monthCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:monthCollectionLayout];
[self addSubview:_monthCollection];
[_monthCollection.topAnchor constraintEqualToAnchor:self.topAnchor constant:65].active = YES;
[_monthCollection.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:2].active = YES;
[_monthCollection.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:0].active = YES;
[_monthCollection.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:0].active = YES;
这是我添加的 UIView
UIView *cursor = [[UIView alloc] init];
cursor.backgroundColor = [UIColor colorWithHexString:@"#E66163" setAlpha:.4];
cursor.layer.borderColor = [UIColor colorWithHexString:@"#E66163" setAlpha:1].CGColor;
cursor.layer.borderWidth = 1;
cursor.layer.cornerRadius = 15;
cursor.translatesAutoresizingMaskIntoConstraints = NO;
[_monthCollection addSubview:cursor];
[cursor.topAnchor constraintEqualToAnchor:_monthCollection.topAnchor constant:0].active = YES;
[cursor.leftAnchor constraintEqualToAnchor:_monthCollection.leftAnchor constant:0].active = YES;
[cursor.widthAnchor constraintEqualToConstant:_monthCollection.frame.size.width].active = YES;
[cursor.bottomAnchor constraintEqualToAnchor:_monthCollection.bottomAnchor constant:0].active = YES;
我需要 UIVIEW 的宽度等于 _monthCollection.frame.size.width/3
我尝试了所有方法,但似乎我不在乎任何事情..我无法得到任何结果......
所以我想知道......当我使用 anchor 约束时如何插入宽度?
最佳答案
您使用了错误的方法来创建约束。您不想将其设置为常量,而是希望将 View 的宽度限制为集合的宽度。我不会“说” ObjC,但我相信您可以轻松地将我的 Swift 代码重写为 objC:
cursor.widthAnchor.constraint(equalTo: _monthCollection.widthAnchor, multiplier: 0.33).isActive = true
关于ios - 从 Collection View 宽度设置 Anchor Constraint 中的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669751/