LoginSignup
5

More than 5 years have passed since last update.

UICollectionViewCellのAutoLayoutが効かない(未解決?)

Posted at

UICollectionViewCellのItemSizeを動的に変えた場合(デバイスを回転させたときにItemSizeを変更した)にUICollectionViewCellのAutoLayoutが効かなくなる。という現象に遭遇しました。(そもそもItemSizeを途中で変えるというアプローチがダメな気がしますが・・・。)

とりあえず、解決策としては - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPathでUICollectionViewCellが持っているcontentViewに対してAutoLayoutの設定を書いてやれば解決しました。

contentViewにAutoLayout設定

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    //Masonryを使っているのでAutoLayoutは以下のような記述
    [cell.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(cell);
    }];

    //do something

    return cell;
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5