【发布时间】: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