【发布时间】:2020-06-30 12:24:56
【问题描述】:
我发现了一些奇怪的东西。
我在其中创建了一个自定义 UICollectionViewCell 和一个标签。
我只对标签的顶部、前导和底部应用了自动布局。
class TestCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
如果文本很长,则会像下面那样从屏幕上剪掉。
如果文本太长,我希望文本显示为“...”。
所以我应用了这样的自动布局。
然后,collectionViewCell 宽度被打破。
这是我的代码。
我已经设置了委托,数据源。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? TestCollectionViewCell else { return UICollectionViewCell() }
cell.label.text = "asdfasdkfjabsdkljfbasdfhaoweihf;oaisefowiejfao;wihfaksdjfhakdjf;lskdjfa;zxknb"
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.width - 1) / 2, height: 53)
}
}
【问题讨论】:
-
您是否尝试过将collectionview中的单元格估计大小设置为none?
标签: ios swift uicollectionview autolayout uicollectionviewcell