UICollectionViewCell大小适合设备旋转的所有单元格

问题描述 投票:0回答:3

我有一个寻呼机全屏收集视图。当设备旋转时,collectionview的第一个单元格适合于横向和纵向模式的collectionview。但是当我在collectionview上滚动下一个单元格时,它显示了两个单元格。它并不适合所有的屏幕。虽然我滚动collectionview并旋转,但我如何将所有单元格大小适合collectionview。我试过invalideLayout()和很多方法。但我无法实现。我只是想要,尽管滚动和旋转,collectionviewcell在任何情况下都适合整个屏幕。

P.S collectionviewcell大小应该是整个屏幕大小。

public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.height
        layout.itemSize = CGSize(width: width, height: UIScreen.main.bounds.width)
        layout.invalidateLayout()
    } else if UIDevice.current.orientation.isPortrait,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.width
        layout.itemSize = CGSize(width: width , height:  UIScreen.main.bounds.height)
        layout.invalidateLayout()
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let width = view.frame.size.width
    return CGSize(width: width , height: UIScreen.main.bounds.height)
}
ios swift uicollectionview uicollectionviewcell uicollectionviewlayout
3个回答
0
投票

确保删除单元格之间的间距,如下所示。这将解决您的问题。

enter image description here


0
投票

你必须在viewWillLayoutSubviews()中编写你的代码。像这样

override func viewWillLayoutSubviews(){super.viewWillLayoutSubviews()

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



    if UIDevice.current.orientation == .landscapeLeft
    {
        flowLayout.itemSize = CGSize(width: self.view.frame.width, height: 500)
    }
    else if UIDevice.current.orientation == .landscapeRight
    {
        flowLayout.itemSize = CGSize(width: self.view.frame.width, height: 500)
    }

    flowLayout.invalidateLayout()
}

0
投票

你可以尝试应用以下方法吗?

第一种方法

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

   super.viewWillTransition(to: size, with: coordinator)

   // YOUR CODE OR FUNCTIONS CALL HERE

}

2'方法

override func viewDidLoad() {
    super.viewDidLoad()        

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}

deinit {
     NotificationCenter.default.removeObserver(self)
}

func rotated() {
    if UIDevice.current.orientation.isLandscape,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.height
        layout.itemSize = CGSize(width: width, height: UIScreen.main.bounds.width)
        layout.invalidateLayout()
    } else if UIDevice.current.orientation.isPortrait,
        let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        let width = view.frame.width
        layout.itemSize = CGSize(width: width , height:  UIScreen.main.bounds.height)
        layout.invalidateLayout()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.