简体   繁体   English

Swift:Collection在旋转后查看更改布局

[英]Swift: Collection View changing layout after rotation

I have a collection view with 6 items and want to display them in a 2 cells per row and 3 rows format. 我有一个包含6个项目的集合视图,并希望以每行2个单元格和3行格式显示它们。 The following code achieves this (as taken from this question: Swift: Collection View not adjusting correctly to change in size ) in iPhone format nicely. 下面的代码很好地实现了这个问题(从这个问题: Swift:Collection View无法正确调整以改变大小 )。

However on the any iPad the views layout is correct initially but if the screen is rotated to landscape and then back to portrait then the layout does not fully fit within the view and requires horizontal scrolling to see the second cell (cells width has somehow increased meaning the second cell in each row is partially cut off). 但是在任何iPad上,视图布局最初是正确的,但如果屏幕旋转到横向然后回到纵向,则布局不完全适合视图,并且需要水平滚动才能看到第二个单元格(单元格宽度在某种程度上增加了意义每行中的第二个单元被部分切断)。

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)
}

轮换前 轮换后

The problem here appears to be that, frame of the view is not updated when 'willRotate' is invoked. 这里的问题似乎是,当调用'willRotate'时,视图的框架不会更新。 Instead try 'didrotate' or add the observer 'NSNotification.Name.UIDeviceOrientationDidChange' and invalidateLayout of collection inside the selector method. 而是尝试'didrotate'或在selector方法中添加观察者'NSNotification.Name.UIDeviceOrientationDidChange'和invalidateLayout of collection。 Don't forget to remove the observer on deinit(). 不要忘记删除deinit()上的观察者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM