5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

向きが変わった後に CollectionView のセルのサイズの変更を適用する

Posted at

カスタムキーボードの中で UICollectionView を使用している場合に、デバイスの向きが変わった後にセルのサイズを変更することができず、 landscape モードでレイアウトが崩れていた。

カスタムキーボード特有の話なのかわからないが、デバイスの向きが変わった後に - collectionView:layout:sizeForItemAtIndexPath: は呼ばれなくなり、セルのサイズの計算が portrait モードから変わってないのが原因だった。

それが何故なのかは分からないが、向きが変わった後に下記のメソッドを呼び出すことで解決された。

collectionView.performBatchUpdates(nil, completion: nil)

挿入、削除、読み込みなどを行うときに呼ぶことで、複数のアニメーションを一括で実行できるらしい。それだけだと今回の問題に適用できるようには読めないが、リファレンスによると下記の通り use it to change the layout parameters associated with one or more cells. とあるので、この部分が該当するのかな。

You can use this method in cases where you want to make multiple changes to the collection view in one single animated operation, as opposed to in several separate animations. You might use this method to insert, delete, reload, or move cells or use it to change the layout parameters associated with one or more cells. Use the blocked passed in the updates parameter to specify all of the operations you want to perform.

5
5
0

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
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?