![](/img/trans.png)
[英]UICollectionViewController: make sticky footer, regardless of size of CollectionView
[英]How to fix CollectionView cell's size and make an auto layout regardless of the initial state of the Simulator?
設置collectionView單元格寬高的代碼如下:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/6, height: collectionView.frame.width/5)
}
單元格的大小將隨着模擬器的初始狀態而變化。 我認為原因是collectionView.frame.width
取決於模擬器的狀態。
無論ipad的初始狀態如何,如何使用自動布局像代碼一樣修復單元格的大小?
解決這個問題的最好和最簡單的方法是將其設為 sqaure 而不是 Rectangle 。 在我看來,寬度/高度基本上具有相同的值。 如果它不能幫助您嘗試下面的代碼,那么對您的代碼進行一個小的更改是使用 bounds 而不是 frames 。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width/5, height: collectionView.bounds.width/5)
}
此外,如果你能猜出bounds.width/5 -10
之間的尺寸邊距,你也可以減少bounds.width/5 -10
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let standardWidth = max(collectionView.frame.width, collectionView.frame.height)
return CGSize(width: standardWidth/5, height: standardWidth/5)
}
這個簡單的解決方案效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.