gpt4 book ai didi

swift - collectionViewCells 到 collectionView 位置 x :0, y:0

转载 作者:行者123 更新时间:2023-11-30 12:27:47 27 4
gpt4 key购买 nike

我遇到了 collectionViewCells 的一些布局问题。我以编程方式应用布局约束,并且 UICollectionViewCells 没有固定到我的 collectionView 中的 0,0 位置。请参阅随附的屏幕截图以供引用。提前致谢!

class CurrentUserPlaceDetailsVC: UIViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
super.viewDidLoad()

setupMenuBar()
}

let menuBar: MenuBar = {
let mb = MenuBar()
return mb
}()

private func setupMenuBar() {
view.addSubview(menuBar)
view.addConstraintsWithFormat("H:|[v0]|", views: menuBar)
view.addConstraintsWithFormat("V:|-64-[v0(150)]", views: menuBar)

}
var placesTableVC: PlacesTableVC?
}

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.green
cv.dataSource = self
cv.delegate = self
return cv
}()

let cellId = "cellId"

override init(frame: CGRect) {
super.init(frame: frame)


collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)

addSubview(collectionView)
addConstraintsWithFormat("H:|[v0]|", views: collectionView)
addConstraintsWithFormat("V:|[v0]|", views: collectionView)

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = .blue
return cell
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}


extension UIView {

func addConstraintsWithFormat(_ format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDictionary[key] = view
}

addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}

enter image description here

最佳答案

您需要设置您使用的 UICollectionViewFlowLayout 实例的 minimumLineSpacingheaderReferenceSize 和/或 sectionInset初始化您的UICollectionView。您还可以设置 UICollectionViewcontentInset

试试这个:

lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.headerReferenceSize = .zero
layout.sectionInset = .zero
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .green
cv.dataSource = self
cv.delegate = self
cv.contentInset = .zero
return cv
}()

关于swift - collectionViewCells 到 collectionView 位置 x :0, y:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43958793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com