我有collectionView
,但拥有观看权限。我想得到iPhone X的结果:
但是我明白了:
如何解决?
代码:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
cellOffset = 10
cellWidth = (667 / 3) - (cellOffset * 4)
cellHeight = (cellWidth / 2 * 3) + (cellWidth / 2 * 0.65)
return CGSize(width: cellWidth, height: cellHeight)
}
更新
更新
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let sectionInset = collectionView.frame.width / 3.0
let margins = CGFloat(10.0 * 2.0)
let width = (collectionView.frame.width - sectionInset - margins) / 3.0
let height = collectionView.frame.height - 40.0
let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
collectionViewLayout?.sectionInset = UIEdgeInsets(top: 0, left: 6.0, bottom: 0, right: 6.0)
collectionViewLayout?.minimumInteritemSpacing = 10
collectionViewLayout?.invalidateLayout()
return CGSize(width: width, height: height)
}
结果:
答案 0 :(得分:1)
如果要在横向放置3个单元格
let cellWidth = ( self.collectionView.frame.width - 2 * cellOffset ) / 3
答案 1 :(得分:0)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/4 - 1, height: collectionView.frame.size.width/4 - 1)
}
答案 2 :(得分:0)
我的建议是考虑重构您的布局,以使每个部分在您要查找的边距左右插入。然后,您可以通过以下方式描述您的单元格大小:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let sectionInset = collectionView.frame.width / 3.0
let margins = CGFloat(10.0 * 2.0)
let width = (collectionView.frame.width - sectionInset - margins) / 3.0
let height = collectionView.frame.height - 40.0
return CGSize(width: width, height: height)
}
接下来,您要在适当的委托方法中返回此部分的插入值。假设您要保留该收集视图的前六个和后第六个保留空间,这大致就是您的图像显示的内容。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0,
left: collectionView.frame.width / 6.0,
bottom: 0,
right: collectionView.frame.width / 6.0)
}
将所有内容放在一起,最终得到此修订:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let sectionInset = collectionView.frame.width / 3.0
let margins = CGFloat(10.0 * 2.0)
let width = (collectionView.frame.width - sectionInset - margins) / 3.0
let height = collectionView.frame.height - 40.0
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0,
left: collectionView.frame.width / 6.0,
bottom: 0,
right: collectionView.frame.width / 6.0)
}
答案 3 :(得分:0)
您需要通过该视图设置集合视图的布局。
// setup collection view
let numberItemPerRow = 4
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 1, left: 0, bottom: 1, right: 0)
layout.itemSize = CGSize(width: ScreenSize.width/numberItemPerRow-1,
height: ScreenSize.width/numberItemPerRow)
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1
your_collection.collectionViewLayout = layout
我保证此代码将被解决。 !!
答案 4 :(得分:0)
您使它有点太复杂了。这只是要玩一些插图和大小。这是示例代码:
tabs.executeScript
这是它所提供的图像:
更新
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 145, 0, 145)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
indePath = indexPath
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.frame.width / 3 - 100 , height: collectionView.frame.size.height - 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}