旋转Lanscape集合查看

时间:2018-07-24 08:08:05

标签: ios swift

我使用集合视图显示了10个带有肖像的单元格:

enter image description here

旋转时:

enter image description here

我只是尝试代码

override func viewWillLayoutSubviews() {
  super.viewWillLayoutSubviews()

  guard let followLayout = demoView.collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
  return }

  followLayout.itemSize = UIScreen.main.bounds.size

  followLayout.invalidateLayout()

  view.setNeedsLayout()
}

但不影响。

  

我只是尝试

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)

guard let followLayout = detailView.collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
  return
}

followLayout.invalidateLayout()

let offset = detailView.collectionView.contentOffset
let width = detailView.collectionView.bounds.size.width

let index = round(offset.x / width)
let newOffset = CGPoint(x: index * size.width, y: offset.y)

detailView.collectionView.setContentOffset(newOffset, animated: false)

coordinator.animate(alongsideTransition: { (context) in
  //      self.demoView.collectionView.reloadData()
  self.detailView.collectionView.setContentOffset(newOffset, animated: false)
}, completion: nil)

}

这对我有用。谢谢大家

1 个答案:

答案 0 :(得分:0)

我建议您将每个单元格居中对齐

雨燕4

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

  let totalCellWidth = CellWidth * CellCount

  let totalSpacingWidth = CellSpacing * (CellCount - 1)

  let leftInset = (collectionViewWidth - CGFloat(totalCellWidth + totalSpacingWidth)) / 2

  let rightInset = leftInset

  return UIEdgeInsetsMake(0, leftInset, 0, rightInset)

}