2017-07-07 39 views
0

在我的项目中,我的collectionview单元格包含整个屏幕宽度和高度,我将其设置为水平滚动并为所有屏幕大小设置动态,我使用此方法CollectionView所有屏幕大小的单元格动态高度

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let height: CGFloat = view.frame.size.height 
    let width: CGFloat = view.frame.size.width 

     return CGSize(width: collectionView.bounds.size.width, height: collectionView.bounds.size.height) 

它的工作非常好,照顾所有屏幕大小的宽度和高度,但问题是,在滚动它从右边缘切割屏幕,我试着用不同的方法实现它viewDidLayoutSubViews() uicollectionviewdelegateflowlayout,它也照顾屏幕大小,但仍然有同样的问题。第一个图像是它应该出现的原始单元格。

original cell

下一个图像是如何切断右侧边缘上滚动。最初,我有静态numberofIteminSection,并继续每次刷卡切割。这是第三次或第四次刷卡的结果。我已启用分页,所以我不知道这里的问题是什么。

enter image description here

我已经检查了堆栈溢出对此不同的岗位,但似乎没有任何帮助,我被卡住。任何帮助将不胜感激。

回答

0

您需要检查的屏幕尺寸在sizeForItemAtIndexPath,如:

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

     let screenSize: CGRect = UIScreen.main.bounds 
     let screenWidth = screenSize.width 

     if screenWidth == 414{ 
      return CGSize(width: collectionViewTable.bounds.size.width - 4, height: collectionViewTable.bounds.size.height - 50) 

     }else if screenWidth == 375{ 

      return CGSize(width: collectionViewTable.bounds.size.width - 4, height: collectionViewTable.bounds.size.height - 50) 
     }else{ 

      return CGSize(width: collectionViewTable.bounds.size.width - 4, height: collectionViewTable.bounds.size.height - 50) 
     } 
    } 

添加这些功能也和定制根据您的需要:

func collectionView(_ collectionView: UICollectionView, 
          layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
      return UIEdgeInsetsMake(5, 14, 5, 14) 
     } 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { 
      return 4; 
     } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
     return 4; 
    } 

如果不工作,然后在改变UIEdgeInsetsMakeinsetForSectionAt

+0

@UsamabinAttique完成这项工作? –

相关问题