gpt4 book ai didi

swift - 单元格的大小不等于 Collection View 的大小

转载 作者:行者123 更新时间:2023-11-30 10:54:04 25 4
gpt4 key购买 nike

我编写了一段代码,我得到了包含 4 个单元格的 CollectionView,我可以滑动查看其中的每个单元格。

我的collectionview背景颜色是红色,在我的单元格中我放置了一个图像(在我放置蓝色而不是图像之前),我遇到了一个问题,我在单元格中的 Collection View 中看到了小红线。

我必须说,我遇到了问题,在那之前我有更大的线,我通过隐藏 View Controller 的导航 View 来解决这个问题。

class CheckViewController: UIViewController, UICollectionViewDataSource,
UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .red
cv.dataSource = self
cv.delegate = self
cv.isPagingEnabled = true
return cv
}()


let cellId = "cellId"

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)

view.addSubview(collectionView)


// collectionView.frame = view.frame

//use autolayout instead

collectionView.anchorToTop(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)

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


}


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


// func numberOfSections(in collectionView: UICollectionView) -> Int {
// return 4
// }
//

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


// cell.backgroundColor = .white
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}




}

extension UIView {
func anchorToTop(top: NSLayoutYAxisAnchor? = nil, left:
NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right:
NSLayoutXAxisAnchor? = nil) {

anchorWithConstantsToTop(top: top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0)
}
func anchorWithConstantsToTop(top: NSLayoutYAxisAnchor? = nil, left:
NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right:
NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0,leftConstant:
CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0)
{
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: topConstant).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: bottomConstant).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: leftConstant).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: rightConstant).isActive = true
}

}
}

这是 image .

最佳答案

有两个问题

1) 您正在设置相对于 View 而不是 CollectionView 的单元格大小。

您设置单元格大小时出错..

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}

2) View 的背景图像似乎不适合 100%。您想使用另一个纵横比来填充 View 。您可能想使用 .scaleToFill。

Difference between UIViewContentModeScaleAspectFit and UIViewContentModeScaleToFill?

关于swift - 单元格的大小不等于 Collection View 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54151494/

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