[英]swift: CollectionView cell size
I have collectionView
with constraint to superview. 我有一个带有约束的collectionView
到superview。 I want to get this result for iPhone X: 我想为iPhone X获得此结果:
But I get this: 但我明白了:
How to fix it? 怎么解决?
code: 码:
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)
}
Update 更新
Update 更新
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)
}
result: 结果:
如果你想在景观中有3个细胞
let cellWidth = ( self.collectionView.frame.width - 2 * cellOffset ) / 3
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)
}
My suggestion would be to consider refactoring your layout a tad such that each section is inset on the left and right by the margin you're looking for. 我的建议是考虑重构您的布局,使每个部分左右嵌入您正在寻找的边距。 You could then describe your cell size in the following manner: 然后,您可以按以下方式描述单元格大小:
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)
}
Next you'd want to return this section inset value in the appropriate delegate method. 接下来,您需要在适当的委托方法中返回此部分的插入值。 Let's assume you want to reserve the first and last sixth of the collection view for this margin, which is roughly what your image shows. 假设您要保留此边距的集合视图的第一个和最后六个,这大致与图像相同。
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)
}
Putting it all together, we end up with this revision: 总而言之,我们最终得到了这个修订:
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)
}
You need to set layout of collection view by this one. 您需要通过此设置集合视图的布局。
// 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
I guarantee that this code will be solved. 我保证这段代码会被解决。 !! !
You are making it a bit too complicated. 你让它有点太复杂了。 It's just about playing a bit with the insets and the size. 它只是与插图和大小一起玩。 Here is the example code: 这是示例代码:
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
}
Here is the image of what it gives: 这是它给出的图像:
Update 更新
import UIKit
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {
lazy var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .green
cv.isPagingEnabled = true
cv.delegate = self
cv.dataSource = self
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(collectionView)
collectionView.register(Cell.self, forCellWithReuseIdentifier: "cell")
collectionView.frame = self.view.frame
collectionView.contentInset = UIEdgeInsetsMake(0, -45, 0, -45)
}
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 {
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
}
}
class Cell : UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
self.backgroundColor = .red
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.