【问题标题】:How to add auto layout on UICollectionView programmatically?如何以编程方式在 UICollectionView 上添加自动布局?
【发布时间】:2015-06-10 12:44:17
【问题描述】:

我以编程方式创建了一个uicollectionview,并且我想在集合视图上添加自动布局约束,以便以后可以更改框架。但是下面的代码是行不通的。

我正在尝试像这样初始化集合视图:

        self.rootCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, self.bounds.size.width, self.bounds.size.height - 100) collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];
        self.rootCollectionView.dataSource = self;
        self.rootCollectionView.delegate = self;
        self.rootCollectionView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.rootCollectionView];
        [self.rootCollectionView registerClass:[CASlideSwitchViewCell class] forCellWithReuseIdentifier:@"CASlideSwitchViewCell"];
        self.topConstraint = 100;
        float topCon = self.topConstraint;//self.topConstraint is a CGFloat property,
        UICollectionView * cv = self.rootCollectionView;
        cv.translatesAutoresizingMaskIntoConstraints = NO;
        NSArray * constraintArray = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[cv]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(cv)];
        NSArray *constraintArray2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-topCon-[cv]-0-|" options:0 metrics:@{@"topCon":@(topCon)} views:NSDictionaryOfVariableBindings(cv)];
        [self addConstraints:constraintArray];
        [self addConstraints:constraintArray2];

然后在收到数据后,我尝试根据数据更改 topConstraint。像这样:

if (number <= 1) {
    self.topScrollView.hidden = YES;
    self.topConstraint = 0;
}else{
    self.topScrollView.hidden = NO;
    self.topConstraint = 100;
}
[self updateConstraints];
[self.rootCollectionView.collectionViewLayout invalidateLayout];
[self.rootCollectionView reloadData];

但是,UICollectionView 的上边距始终为 100。

我错过了什么吗?或者这样添加自动布局是不对的?

【问题讨论】:

  • 尝试从笔尖添加自动布局,这样会很容易
  • @nischalhada 我知道 nib 很容易使用,但是在这个项目中,我不允许使用 nib。

标签: ios objective-c autolayout uicollectionview


【解决方案1】:

self.topConstraint 是浮点数而不是 NSLayoutConstraint,因此您无法更新约束。您可以代替使用

NSArray *constraintArray2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-topCon-[cv]-0-|" options:0 metrics:@{@"topCon":@(topCon)} views:NSDictionaryOfVariableBindings(cv)];

你应该使用

NSLayoutConstraint *topLayoutContraint = [NSLayoutConstraint constraintWithItem:cv
                             attribute:NSLayoutAttributeTop
                             relatedBy:NSLayoutRelationEqual
                                toItem:self
                             attribute:NSLayoutAttributeTop
                            multiplier:1.0
                              constant:100];

NSArray *constraintArray2 = @[
                              topLayoutContraint,
                              [NSLayoutConstraint constraintWithItem:cv
                                                           attribute:NSLayoutAttributeBottom
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:self
                                                           attribute:NSLayoutAttributeBottom
                                                          multiplier:1.0
                                                            constant:0]
                              ];

那么你可以

if (number <= 1) {
    self.topScrollView.hidden = YES;
    topLayoutContraint.constant = 0;
} else {
    self.topScrollView.hidden = NO;
    topLayoutContraint.constant = 100;
}

[self setNeedsLayout];
[self.rootCollectionView reloadData];

【讨论】:

  • 约束是正确的,但它似乎不适用于以编程方式创建的 UICollectionView。
  • self.rootCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, self.bounds.size.width, self.bounds.size.height-100) collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]]; NSLayoutConstraint *topLayoutContraint = [NSLayoutConstraint constraintWithItem:cv attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
  • 我已经尝试了上面的代码,它创建了 100 边距,并立即添加了 0 边距的约束,但它仍然不起作用。
  • 我为我的小错误感到抱歉。你应该使用topLayoutContraint.constant = 0;
  • 顺便说一下,cv.translatesAutoresizingMaskIntoConstraints = NO;不能被删除
猜你喜欢
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多