I have a cell with image pinned to left-top-right of cell. Other constraints related to image.
Here is image height constraint:
Here is my data setter:
import UIKit
import Kingfisher
class WorkoutSectionCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var colorLine: UIView!
@IBOutlet weak var workoutName: UILabel!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!
@IBOutlet weak var workoutDescription: UILabel!
@IBOutlet weak var parametersStackView: UIStackView!
@IBOutlet weak var exercisesIcon: UIImageView!
@IBOutlet weak var exercisesLabel: UILabel!
@IBOutlet weak var musclesIcon: UIImageView!
@IBOutlet weak var musclesLabel: UILabel!
@IBOutlet weak var workoutImage: UIImageView!
var workout: Workout? {
didSet {
// Get image form backend
if let imageUrl = workout?.workoutImage?.imageUrl {
let resource = ImageResource(downloadURL: URL(string: imageUrl)!, cacheKey: imageUrl)
workoutImage.kf.setImage(with: resource)
workoutImage.isHidden = false
} else {
workoutImage.isHidden = true
}
if let workoutKind = workout?.workoutKind {
switch workoutKind {
case "силовая": colorLine.backgroundColor = Colors.colorCadmiumOrange
case "фитнес": colorLine.backgroundColor = Colors.colorGreen
case "кардио": colorLine.backgroundColor = Colors.colorBlueDeFrance
case "HIIT": colorLine.backgroundColor = Colors.colorCarminePink
default:
colorLine.backgroundColor = Colors.colorClear
}
}
workoutName.text = workout?.workoutName
if let workoutDesc = workout?.workoutDesc {
workoutDescription.text = workoutDesc.html2String
}
if let numberOfExercises = workout?.workoutExercises?.count {
exercisesLabel.text = "\(numberOfExercises) " + pluralForm(number: numberOfExercises, forms: ["упражнение", "упражнения", "упражнений"])
}
var workoutMuscles: [String] = []
workout?.workoutExercises?.forEach({ (exercise) in
workoutMuscles.append((exercise as! Exercise).mainMuscle!)
})
musclesLabel.text = Set(workoutMuscles).joined(separator: ", ").lowercased()
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.contentView.translatesAutoresizingMaskIntoConstraints = false
let screenWidth = UIScreen.main.bounds.size.width
widthConstraint.constant = screenWidth - 20
self.layer.cornerRadius = 3
workoutName.textColor = Colors.colorPaynesGrey
workoutDescription.textColor = Colors.colorDimGray
exercisesLabel.textColor = Colors.colorSilver
musclesLabel.textColor = Colors.colorSilver
musclesIcon.tintColor = Colors.colorSilver
exercisesIcon.tintColor = Colors.colorSilver
}
I use KingFisher library to get image from backend by REST Api.
The problem is some cells have image and then everything is ok, but some cells haven't image and then empty space appear.
I would like to hide empty space if no image and shrink cell height to fit content. How I can do it?
SOLVED: Here is how I solve it wit help of @Fangming Ning
I add an outlet for image height:
@IBOutlet weak var imageHeightConstraint: NSLayoutConstraint!
Then set window width and insets (by 10 left & right):
let screenWidth = UIScreen.main.bounds.size.width
let screenInsets: CGFloat = 20
And in didSet add following code:
if let imageUrl = workout?.workoutImage?.imageUrl {
let resource = ImageResource(downloadURL: URL(string: imageUrl)!, cacheKey: imageUrl)
workoutImage.kf.setImage(with: resource)
// Set image height if image exists
imageHeightConstraint.constant = (screenWidth - screenInsets) * 0.666
} else {
// Set 0 if no image
imageHeightConstraint.constant = 0
}