我使用 collectionViewCell 創建了一個選項卡選單,但是我有 5 個單元格選單。但是單元格中的名稱被截斷了,我想要是否有長名稱標簽。collectionViewCell 中的容器視圖應該增長。

我嘗試使用 collectionViewFlowLayout estimatedItemSize,但結果是。細胞變得丑陋不像以前

如何使 collectionViewCell 變大但保持全寬。
這是我的 collectionViewCell 設定
private let padding: CGFloat = 8
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func configure() {
addSubview(selectionView)
selectionView.addSubview(titleLabel)
NSLayoutConstraint.activate([
selectionView.topAnchor.constraint(equalTo: topAnchor, constant: padding),
selectionView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 4),
selectionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -4),
selectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -padding),
titleLabel.topAnchor.constraint(equalTo: selectionView.topAnchor, constant: padding),
titleLabel.leadingAnchor.constraint(equalTo: selectionView.leadingAnchor, constant: 4),
titleLabel.trailingAnchor.constraint(equalTo: selectionView.trailingAnchor, constant: -4),
titleLabel.bottomAnchor.constraint(equalTo: selectionView.bottomAnchor, constant: -padding)
])
}
這是我的 collectionView 設定
private func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
collectionView = UICollectionView(frame: bounds, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(TabCollectionViewCell.self, forCellWithReuseIdentifier: TabCollectionViewCell.cellID)
[notificationLabel, collectionView].forEach { view in
guard let view = view else { return }
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
}
NSLayoutConstraint.activate([
notificationLabel.topAnchor.constraint(equalTo: topAnchor, constant: 8),
notificationLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding),
notificationLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding),
collectionView.topAnchor.constraint(equalTo: notificationLabel.bottomAnchor, constant: 8),
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding),
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding),
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
])
}
extension HeaderCollectionView: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width / CGFloat(items.count), height: collectionView.bounds.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
uj5u.com熱心網友回復:
創建一個 String 擴展方法,它將計算字串的寬度,然后在您的委托方法中使用它:
第一步:計算字串寬度
extension String {
func getStringwidth(height: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
let boundingBox = self.boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedString.Key.font: font], context: nil)
return boundingBox.width
}
}
第 3 步:計算最大寬度
func calculateMaxWidth()-> CGFloat {
// my Data shource array Data
var allSize: [CGFloat] = []
arrayData.forEach{
allSize.append($0.widthWithConstrainedHeight(height: 30,
font: UIFont.systemFont(ofSize: 17)))
}
return allSize.max() ?? 0.0
}
第 2 步:從 viewdidLoad 呼叫 max
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: calculateMaxWidth(), height: 30)
根據您的要求進行調整。
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/536718.html
標籤:迅速用户界面集合视图uicollectionviewcell
