2017-09-29 36 views
2

在我的應用程序中,我以模態方式呈現視圖控制器,並且此視圖控制器包含填充超視圖的UICollectionView。集合視圖中的每個單元格都需要全屏 - 與視圖的邊界大小相同。爲了達到這個目的,我只需將UICollectionViewFlowLayoutitemSize設置爲viewDidLayoutSubviews中視圖的邊界大小。橫屏上的iPhone X上的全屏UICollectionViewCell問題

這對於所有設備和方向非常有效,除了橫向的iPhone X。在這種情況下,當呈現該屏幕時,看起來安全區域佈局邊界正在發揮作用並影響該單元的定位。該單元從顯示器的左邊緣推出,並向上推,以使單元格的底部與原位指示器的垂直中心對齊。使用View Debugger檢查佈局顯示集合視圖填充屏幕,但單元格的Y位置爲-10.67,X位置爲0,即使它位於集合視圖的前端。值得注意的是,這被記錄在控制檯上:

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, minus the content insets top and bottom values. 
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa6311253b0>, and it is attached to <UICollectionView: 0x7fa63207e800; frame = (0 0; 812 375); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x608000244d70>; layer = <CALayer: 0x60800023ef00>; contentOffset: {-44, 0}; contentSize: {0, 812}; adjustedContentInset: {0, 44, 21, 44}> collection view layout: <UICollectionViewFlowLayout: 0x7fa6311253b0>. 

這裏發生了什麼事情?如何解決這個問題?

+0

只是好奇,爲什麼不設置itemSize'的CollectionView的內部(_:佈局:sizeForItemAt:)''代替的viewDidLayoutSubviews'? – damiancesar

+0

另外,如果設置itemSize與第一個提到的,請確保使用'view.frame'的寬度和高度,而不是'view.bounds'。 – damiancesar

回答

8

默認情況下,iOS 11自動調整橫向版iPhone X的內容插頁,導致發生此問題。要解決該問題,您可以簡單地禁用自動內容嵌入調整,因爲在這種情況下不需要也不需要。

collectionView.contentInsetAdjustmentBehavior = .never

+0

工作完美!我試圖使用'UICollectionViewFlowLayoutSectionInsetReference'來關閉它,但似乎在做這件事以外的事情。 – sahandnayebaziz

+1

嗨,我已經把這個行爲添加到我在didSet觀察者的集合中,我將'cellView size(_:layout:sizeForItemAt:)設置爲像'return self.view.frame.size'一樣的單元大小, m仍然只在iPhone X的環境下出現問題。還有其他事情我應該檢查嗎? – Jose