Landscape orientation for Collection View in Swift

前端 未结 4 2229
终归单人心
终归单人心 2021-01-23 03:38

I am encountering a problem on landscape orientation for my collection view cell. When the app is in portrait it gives me the correct number of cell per row which is 2. But when

4条回答
  •  日久生厌
    2021-01-23 04:09

    Swift 5

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        var flag:CGSize? = nil
        if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad
        {
            if UIApplication.shared.statusBarOrientation.isPortrait{
    
                let cellWidth = floor((collectionView.bounds.width - 5) / 2)
                flag = CGSize(width: cellWidth, height: cellWidth)
            }
    
            else if UIApplication.shared.statusBarOrientation.isLandscape{
    
                let cellwidth = floor((collectionView.bounds.width - 5) / 4)
                flag =  CGSize(width: cellwidth, height: cellwidth)
    
            }
    
        }
        else if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone{
    
            if UIApplication.shared.statusBarOrientation.isLandscape {
              let cellWidth = floor((collectionView.bounds.width - 5) / 2)
                flag = CGSize(width: cellWidth, height: cellWidth)
            } else if UIApplication.shared.statusBarOrientation.isPortrait{
                //  let cellWidth = floor((collectionView.bounds.width - 5))
                flag = CGSize(width: 402 , height: 185)
            }
    
        }
    
        return flag!
    }
    

提交回复
热议问题