gpt4 book ai didi

swift - Uicollectionview 单元格仅环绕文本

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

我的快速代码collectionviewcell位于所有代码中,而不是 Storyboard中。正如你在我下面的照片中看到的。 uicollectionviewcell 包裹在一个数字周围,我希望 collectionviewcell 中有更多的空间。我不知道如何使用下面的代码向 uicollectionviewcell 添加高度约束。简而言之,只需使每个收集单元的高度为 100,宽度为 100,谢谢。

enter image description here

import UIKit

class Cell: UICollectionViewCell {

static var identifier: String = "Cell"

weak var textLabel: UILabel!

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

let textLabel = UILabel(frame: .zero)
textLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(textLabel)
NSLayoutConstraint.activate([
self.contentView.centerXAnchor.constraint(equalTo: textLabel.centerXAnchor),


self.heightAnchor.constraint(equalToConstant: 100),
self.contentView.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor),

])
self.textLabel = textLabel
self.reset()
textLabel.backgroundColor = .yellow
}

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

override func prepareForReuse() {
super.prepareForReuse()
self.reset()
}

func reset() {
self.textLabel.textAlignment = .center
}
}
class ViewController: UIViewController {

weak var collectionView: UICollectionView!

var data: [Int] = Array(0..<10)

override func loadView() {
super.loadView()

let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(collectionView)
NSLayoutConstraint.activate([
self.view.topAnchor.constraint(equalTo: collectionView.topAnchor),
self.view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
self.view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
self.view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
])
self.collectionView = collectionView
}

override func viewDidLoad() {
super.viewDidLoad()

self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(Cell.self, forCellWithReuseIdentifier: Cell.identifier)
self.collectionView.alwaysBounceVertical = true
self.collectionView.backgroundColor = .brown
}
}

extension ViewController: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.data.count
}

func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
let data = self.data[indexPath.item]
cell.textLabel.text = String(data)


return cell
}
}

extension ViewController: UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}
}

extension ViewController: UICollectionViewDelegateFlowLayout {

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

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //.zero
}

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}

最佳答案

更新 sizeForItemAt 方法以返回 w = 100 h = 100

extension ViewController: UICollectionViewDelegateFlowLayout {

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

关于swift - Uicollectionview 单元格仅环绕文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59508712/

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