6個のアイテムのコレクションビューがあり、1行2列3行のフォーマットで表示する必要があります。次のコードは、この形式(これはSwift: Collection View not adjusting correctly to change in size)からうまくiPhone形式でこれを実現しています。Swift:コレクション回転後のレイアウト変更の表示
しかし、任意のiPadでは、最初はビューのレイアウトが正しいですが、画面を横向きにしてから再び縦向きにした場合、レイアウトはビュー内に完全には収まらず、横スクロールして2番目のセル何とか各行の第2のセルが部分的に切断されていることを意味している)。
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
flowLayout.scrollDirection = .horizontal
flowLayout.minimumLineSpacing = 5
flowLayout.minimumInteritemSpacing = 5
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
self.collectionView.collectionViewLayout.invalidateLayout()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if UIDevice.current.orientation.isLandscape {
let totalWidth = collectionView.frame.width
let totalHeight = collectionView.frame.height
let heightOfCell = totalHeight/2
let numberOfCellsPerRow = 1
let widthOfCell = CGFloat(Int(totalWidth)/numberOfCellsPerRow)
return CGSize(width: widthOfCell , height: heightOfCell)
} else {
let totalWidth = collectionView.frame.width
let totalHeight = collectionView.frame.height
let heightOfCell = (totalHeight/3)
let numberOfCellsPerRow = 2
let widthOfCell = CGFloat(Int(totalWidth)/numberOfCellsPerRow)
return CGSize(width: widthOfCell , height: heightOfCell)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 5, 0, 5)
}
私はあなたの問題を再現することはできません - 正確なコードをコピーして貼り付けようとしましたが、縦型 - >横型 - >縦型を回転すると、開始されたコードとまったく同じです。何が起こっているのかをより明確に示すためにスクリーンショットをいくつか表示できますか? –
私はそれらを追加しました、これはiPadで起こっているのですが、iPhoneではありません –
あなたはストーリーボード上の何かを調整しましたか?これは、自分のコードでシーンに新しいコレクションビューを追加した後のレイアウトではありません。 –