ios - 当 UIImageView 有图像时,UICollectionView 自定义单元格 UILabel 丢失
问题描述
我在使用自定义 UICollectionViewCell 时遇到问题。自定义单元格有一个 UILabel 和一个 UIImageView。问题是,当我将图像放在 UIImageView(左截图)上时,同一单元格中的 UILabel 会消失,如下图所示。但是如果 UIImageView 上没有 Image 源,则 UILabel 不会丢失。
这是具有 UICollectionView 的 Viewcontroller 的代码
class CatTowerVeiwController: UIViewController {
@IBOutlet weak var popCountSwitch: UISwitch!
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let switchState = UserDefaults.standard.bool(forKey: UserDataKey.popCountVisibility)
popCountSwitch.setOn(switchState, animated: false)
collectionView.register(UINib(nibName: "CatTowerCell", bundle: nil), forCellWithReuseIdentifier: "cell")
setupFlowLayout(numberOfCells: 2.0)
}
@IBAction func doneButtonClicked(_ sender: UIButton) {
let isLabelVisible = self.popCountSwitch.isOn
UserDefaults.standard.set(isLabelVisible, forKey: UserDataKey.popCountVisibility)
self.dismiss(animated: false)
}
}
extension CatTowerVeiwController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CatTowerCell
// cell.cellImage.image = #imageLiteral(resourceName: "popcat_closed")
cell.cellName.text = "Popcat"
print(cell.cellName.frame.size.height)
print(cell.cellName.frame.size.width)
return cell
}
}
extension CatTowerVeiwController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let leftAndRightPaddings: CGFloat = 45.0
let numberOfItemsPerRow: CGFloat = 2.0
let width = (collectionView.frame.width-leftAndRightPaddings)/numberOfItemsPerRow
return CGSize(width: width, height: width)
}
private func setupFlowLayout(numberOfCells: CGFloat) {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
let halfWidth = UIScreen.main.bounds.width / numberOfCells
flowLayout.itemSize = CGSize(width: halfWidth * 0.8 , height: halfWidth * 0.8)
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = halfWidth * 0.1
self.collectionView.collectionViewLayout = flowLayout
}
}
这是自定义单元格的代码
class CatTowerCell: UICollectionViewCell {
@IBOutlet weak var cellImage: UIImageView!
@IBOutlet weak var cellName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
这就是我想要做的:
解决方案
解决方案是 压缩优先级
由于 UIImageView 太大,UILabel 的高度变为 0
我的解决方案是设置 UILabel 的垂直压缩优先级大于 UIImageView。
推荐阅读
- python - sklearn_crfsuite的参数需要是字节吗?
- java - 使用 JpaRepository 创建只有一个角色的用户
- java - 当我为按钮创建事件侦听器并告诉我它不存在时,我该怎么办?
- react-native - 有天赋的聊天无效日期问题
- android - 使用 Jetpack Compose 构建软件键盘 - 使用 Jetpack Compose 的 IME 输入法
- c# - 在数据模板中获取绑定路径异常
- snowflake-cloud-data-platform - 如何为新的雪花工作表保持相同的连接
- node.js - 更新 npm 后,我的 nodemon 应用程序崩溃了。该怎么办?
- node.js - 猫鼬与副本集的连接总是失败
- azure-active-directory - Graph servicePrincipals API 未返回所有应用程序