ios - 从 Collection View 宽度设置 Anchor Constraint 中的宽度

标签 ios objective-c autolayout constraints

我需要使用 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/

相关文章:

iphone - map View 上的用户可移动点?

ios - iOS8 核心位置有问题

android - 如何返回Future <object>并将其分配给object,flutter

ios - 用户界面标签 : change a text color when click button

ios - 我们如何在自定义表格单元格中实现具有动态内容的动态项目?

ios - 当 UICollectionView 更改布局时覆盖 UIAccessibilityLayoutChangedNotification

ios - 在 iPhone 上查找用户的 Documents 目录的最佳方法是什么?

objective-c - 如何删除具有权限的文件

iPhone 5 无法正常显示应用程序

iOS >> 拖动 View 跳回原始位置 >> Auto Layout 结合 UIPanGestureRecognizer 问题