2015-06-25 137 views
1

我有一個'UICollectionView',我用它通過繼承子類UICollectionViewFlowLayout一次一個地水平分頁多個項目。根據屏幕尺寸設置UICollectionViewFlowLayout的項目大小

爲此,我已經在兩邊都設置了插圖,並且有一個覆蓋的方法來使項目始終位於屏幕的中心。

然後有電池充滿整個屏幕,但我們只是一個和下一個細胞的提示在屏幕上是這樣的:

|-------------------| 
|-| |-----------| |-| 
|x| |xxxxxxxxxxx| |x| 
|x| |xxxxxxxxxxx| |x| 
|x| |xxxxxxxxxxx| |x| 
|x| |xxxxxxxxxxx| |x| 
|x| |xxxxxxxxxxx| |x| 
|-| |-----------| |-| 
|-------------------| 

爲了有我設置的itemSizeprepareLayout

override func awakeFromNib() { 
    let spacingConstant: CGFloat = 16.0 
    self.minimumLineSpacing = spacingConstant * 2 
    self.sectionInset = UIEdgeInsets(top: 30, left: spacingConstant * 3, bottom: 30, right: spacingConstant * 3) 
    self.scrollDirection = .Horizontal 
} 
override func prepareLayout() { 
    super.prepareLayout() 
    if let mySize = self.collectionView?.frame { 
    let size = CGSize(
     width: mySize.width - self.sectionInset.right - self.sectionInset.left, 
     height: mySize.height - self.sectionInset.top - self.sectionInset.bottom 
    ) 
    self.itemSize = size 
    } 
} 

但我不斷收到警告,itemSize必須小於視圖的高度減插入,這正是我在這裏做的。

the behavior of the UICollectionViewFlowLayout is not defined because: 
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values. 

完成這件事的最佳方法是什麼?

回答

0

我有另外一個警告信息,幫助我,顯然會幫助你:

項目高度必須小於UICollectionView 減去部分插圖和谷值的高度,減去內容 插入頂部和底部值。

只需添加以下內容:

let size = CGSize(
     width: mySize.width - self.sectionInset.right - self.sectionInset.left - self.collectionView!.contentInset.left - self.collectionView!.contentInset.right, 
     height: mySize.height - self.sectionInset.top - self.sectionInset.bottom - self.collectionView!.contentInset.top - self.collectionView!.contentInset.bottom 
    )