简体   繁体   English

自动布局约束:无法同时满足约束

[英]Autolayout constraints: Unable to simultaneously satisfy constraints

I have an issue with adding a NSLayoutConstraint. 我在添加NSLayoutConstraint时遇到问题。 I'd like to update the height of an UICollectionView so that all cells fit in the UICollectionView without scrolling. 我想更新UICollectionView的高度,以便所有单元格都适合UICollectionView而无需滚动。 This is because I have put the UICollectionView in a UIScrollView, together with other UI Elements. 这是因为我将UICollectionView和其他UI元素一起放在了UIScrollView中。

I have set the constraints in the interface builder, and I resize the UICollectionView on viewDidLoad, when I know how many items should be displayed. 我已经在界面构建器中设置了约束,并且当我知道应该显示多少个项目时,我在viewDidLoad上调整了UICollectionView的大小。 I do this with 我这样做

[self allBooksCollectionViewSetConstraints];

I have set 我已经设定

[allBooksCollectionView setTranslatesAutoresizingMaskIntoConstraints:NO];

This is my code 这是我的代码

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
    [self allBooksCollectionViewConstraints];
}

-(NSInteger)allBooksCollectionViewHeight
{
    float booksPerRow;
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
        booksPerRow = 6.0;
    }
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
        booksPerRow = 8.0;
    }
    //calculate right height do display all cells
    NSInteger cvHeight = (ceil((float)[allBooksCollectionView numberOfItemsInSection:0]/booksPerRow)*200.0)+49.0;
    return cvHeight;
}

-(void)allBooksCollectionViewSetConstraints
{
    NSInteger cvHeight = [self allBooksCollectionViewHeight];

    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view(%d)]", cvHeight] options:0 metrics:nil views:@{@"view":allBooksCollectionView}]];
}

I have tried removing the UICollectionView constraints from the UIScrollview, but it doesn't change a thing. 我尝试过从UIScrollview中删除UICollectionView约束,但是它并没有改变任何事情。

[scrollView removeConstraints:allBooksCollectionView.constraints];

On orientation change I get the following error: 在方向更改时,出现以下错误:

Unable to simultaniously satisfy constraints ... (
"<NSLayoutConstraint:0x9aa49f0 V:[UICollectionView:0xc0b6000(849)]>",
"<NSLayoutConstraint:0xb2b15d0 V:[UICollectionView:0xc0b6000(1049)]>"
)

Will attempt to recover by breaking constraint <NSLayoutConstraint:0xb2b15d0 V:[UICollectionView:0xc0b6000(1049)]>

But the other constraint needs to be broken! 但是其他约束需要打破! Not this one, because 1049 is cvHeight. 不是这个,因为1049是cvHeight。

How can I fix this? 我怎样才能解决这个问题?

I have tried removing the UICollectionView constraints from the UIScrollview, but it doesn't change a thing. 我尝试过从UIScrollview中删除UICollectionView约束,但是它并没有改变任何事情。

[scrollView removeConstraints:allBooksCollectionView.constraints];

This line of code is wrong. 这行代码是错误的。 None of the constraints returned from collectionView.constraints will be on the scrollview, so this call will do nothing. collectionView.constraints返回的任何约束都不会在scrollview上,因此此调用将不执行任何操作。 You should store the constraints you care about in a property or instance variable: 您应该将您关心的约束存储在属性或实例变量中:

if (collectionViewHeightConstraints)
{
    [scrollView removeConstraints:collectionViewHeightConstraints];
}
collectionViewHeightConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[view(%d)]", cvHeight] options:0 metrics:nil views:@{@"view":allBooksCollectionView}];
[scrollView addConstraints:collectionViewHeightConstraints];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM