我的代码看起来像这样:
UICollectionView *myCollectionView= myCollectionVC.collectionView;
myCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:myCollectionView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[myCollectionView]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(myCollectionView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myCollectionView]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(myCollectionView)]];
这很好用 - 我可以看到集合视图。
但是当我尝试添加另一个子视图时,它会崩溃:
UICollectionView *myCollectionView= myCollectionVC.collectionView;
UIView *newView = [UIView new];
newView.backgroundColor = [UIColor blueColor];
myCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:myCollectionView];
[self.view addSubview:newView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[myCollectionView]-[newView(>=10)]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(myCollectionView,newView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myCollectionView]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(myCollectionView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[newView]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(newView)]];
崩溃时出现以下错误消息:
***因未捕获的异常'NSInternalInconsistencyException'而终止应用,原因:'负大小不是 支持流程 布局'
新视图只是一个空视图 - 但为什么它认为它是负视图。