UICollectionView框架更改

时间:2014-01-10 11:48:47

标签: ios iphone uicollectionview

我在SO上看到的答案似乎都没有回答这个问题。我正在尝试更改UICollectionView的框架。记录时会显示帧更改,但它不会物理更改帧。我没有任何约束。只是改变框架。就这样。我正在创建一个不可滚动的UICollectionView

    _testStringOne = @"Test String";
    _tagOne = [_testStringOne boundingRectWithSize:CGSizeMake(242.0f, 900.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:15.0f]} context:nil];

    _testStringTwo = @"Another Test String";
    _tagTwo = [_testStringTwo boundingRectWithSize:CGSizeMake(242.0f, 900.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:15.0f]} context:nil];

    CGRect collectionFrame = self.collectionView.frame;
    if (_tagOne.origin.x + _tagOne.size.width + _tagTwo.origin.x + _tagTwo.size.width > 242.0f)
    {
        collectionFrame.size.height = 62.0f;
        sectionCount = 2;
        itemCount = 1;
    }
    else
    {
        collectionFrame.size.height = 31.0f;
        sectionCount = 1;
        itemCount = 2;
    }
    self.collectionView.frame = collectionFrame;
    [self.collectionView reloadData];

2 个答案:

答案 0 :(得分:6)

在纵向和横向方向之间切换时遇到了类似的问题。

后工作正常

self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

答案 1 :(得分:2)

我遇到了同样的问题,所以我在viewDidLayoutSubviews方法中更改了框架。希望这有帮助!