Im試圖使用estimatedItemSize動態調整collectionView單元格的大小以適合它們的內容塔我在使用不同的設備尺寸時遇到了一些問題,特別是寬度未調整大小,它只是保持相同尺寸因爲它在界面構建器中。CollectionView estimatedItemSize按預期工作
我已經在iPhone 7上完成了我的界面生成器的東西,所以屏幕寬度是375.在我的故事板中,我有以下內容。
- outerCollectionView釘在上海華的所有4個側面與0餘量的瓦特
- outerCollectionView細胞:339個H:550
該小區內我有:
- innerCollectionView釘扎到單元格的所有4邊,具有0餘量
- innerCollectionView常量w:339 h:550在IB中添加
在我的DataSource和委託類的我已經在viewDidLoad中函數定義我的流程佈局:
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let flow = outerCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.estimatedItemSize = CGSize(width: width - ((width/18.75)), height: height/4)
flow.minimumLineSpacing = (width/37.5)
flow.sectionInset = UIEdgeInsets(top: (width/37.5), left: (width/37.5), bottom: (width/37.5), right: (width/37.5))
和保持「內部」收集我設置一些這些常量的約束自定義單元格類中並定義流佈局大小的物品:
// This is the correct screen size
// let width = innerCollectionView.frame.size.width
let width = UIScreen.main.bounds.size.width
let flow = innerCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionInset = UIEdgeInsetsMake((width/37.5), (width/37.5), (width/37.5), (width/37.5))
flow.itemSize = CGSize(width: (width/6 - (width/37.5)), height: (width/6 - (width/37.5)))
flow.minimumInteritemSpacing = (width/37.5)
flow.minimumLineSpacing = (width/37.5)
innerCollectionHeight.constant = innerCollectionView.collectionViewLayout.collectionViewContentSize.height
innerCollectionWidth.constant = innerCollectionView.collectionViewLayout.collectionViewContentSize.width
現在,這一切工作正常,細胞調整到垂直很好地適應在iPhone模擬器7我的內容,顯示出正確填充一列單元格。但是,當我加大設備大小不調整單元格的寬度。而當我走到一個較小的設備時,我得到了一大堆關於單元格寬度如何變大的Xcode錯誤,因爲它沒有被調整大小,但沒有顯示。 (下面的一些照片)
2017-02-16 12:53:49.632 ParseStarterProject-Swift[26279:394906] The behavior of the UICollectionViewFlowLayout is not defined because:
2017-02-16 12:53:49.632 ParseStarterProject-Swift[26279:394906] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2017-02-16 12:53:49.632 ParseStarterProject-Swift[26279:394906] Please check the values returned by the delegate.
2017-02-16 12:53:49.633 ParseStarterProject-Swift[26279:394906] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7be38920>, and it is attached to <UICollectionView: 0x7db03600; frame = (0 0; 320 504); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7d61a550>; layer = <CALayer: 0x7be95880>; contentOffset: {0, 0}; contentSize: {320, 20}> collection view layout: <UICollectionViewFlowLayout: 0x7be38920>.
所以,很顯然我不能使用
flow.itemSize =
設置在外網孔的大小,因爲我需要的估計的大小來調節的垂直長度和擁抱的內容。我只是無法弄清楚爲什麼寬度不會適合屏幕尺寸?
大尺寸的裝置 - 餘量太大:CV寬度339
iPhone 7我建立它 - 邊距正常:CV寬度339
----編輯-----
試圖覆寫szeForItemAt但沒有改變。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.view.frame.size.width
let height = self.view.frame.size.height
if collectionView == self.outerCollectionView {
return CGSize(width: width - 100, height: height/4)
} else {
return CGSize(width: (width/6 - (width/75)), height: (width/6 - (width/75)))
}
}
---- ---- EDIT
我可以編輯的優選項目抓住細胞。使用高度壓縮的大小選項和設置您自己的尺寸爲寬度,(寬度/ 18.7)爲寬度減去左和右插圖先前定義
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
let attributes = layoutAttributes.copy() as! UICollectionViewLayoutAttributes
let width = UIScreen.main.bounds.size.width
let desiredWidth = width - (width/18.75)
//let desiredWidth = systemLayoutSizeFitting(UILayoutFittingExpandedSize).width
let desiredHeight = systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
attributes.frame.size.width = desiredWidth
attributes.frame.size.height = desiredHeight
return attributes
}
卜此充塞了約束作爲所述圖像中示出的下方,我認爲這可能只是一個腳本,我已經運行了倒UIViews的頂角。但它也填補了細胞,因爲它增加了每條線。
更小的屏幕,類似的問題與細胞: