当 UIImageView 有图像时,UICollectionView 自定义单元格 UILabel 丢失
UICollectionView custom cell UILabel missing when UIImageView has image
我在使用自定义 UICollectionViewCell 时遇到问题。自定义单元格有一个 UILabel 和一个 UIImageView。问题是,当我将图像放在 UIImageView 上时(左截图),同一单元格中的 UILabel 消失,如下图所示。但是如果UIImageView上没有Image source,UILabel是不会丢失的。
这是 Viewcontroller 的代码,其中包含 UICollectionView
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
}
}
这就是我想要做的:
将图像视图的底部锚点约束固定到标签的顶部锚点。
并确保没有冲突约束。
label.topAnchor.constraint(equalTo: imageview.bottomAnchor).isActive = true
解决方案是压缩优先级
因为UIImageView太大,UILabel的高度变成了0
我的解决方案是将 UILabel 的垂直压缩优先级设置为大于 UIImageView。
我在使用自定义 UICollectionViewCell 时遇到问题。自定义单元格有一个 UILabel 和一个 UIImageView。问题是,当我将图像放在 UIImageView 上时(左截图),同一单元格中的 UILabel 消失,如下图所示。但是如果UIImageView上没有Image source,UILabel是不会丢失的。
这是 Viewcontroller 的代码,其中包含 UICollectionView
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
}
}
这就是我想要做的:
将图像视图的底部锚点约束固定到标签的顶部锚点。 并确保没有冲突约束。
label.topAnchor.constraint(equalTo: imageview.bottomAnchor).isActive = true
解决方案是压缩优先级
因为UIImageView太大,UILabel的高度变成了0
我的解决方案是将 UILabel 的垂直压缩优先级设置为大于 UIImageView。