Info Cell을 만들어 본다. 간단하게 View를 정의할 것이다.
VM을 조정하여 더욱 Type safe 하게 만들 것이다.
UI 잡는건 너무 기본적인 거라서 이번에는 설명을 생략하겠다.
system 색상만으로 코딩을 진행하는 게 인상 깊었는데 이렇게 두고 보니 꽤 깔끔한 화면이 나온다.
contentView.layer.masksToBounds에 대해 잠깐 설명하면 뷰의 경계를 기준으로 서브 뷰와 레이어의 영역을 잘라내는지의 여부를 결정하게 된다. 우리는 이걸 true로 하는데 경계를 넘어가는 서브 뷰를 삭제해 버린다. 보통 circle Image View 같은 걸 만들 때 자주 사용할 것이다.
final class RMCharacterInfoCollectionViewCell: UICollectionViewCell {
...
private let valueLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Earth"
label.font = .systemFont(ofSize: 22, weight: .light)
return label
}()
private let titleLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Location"
label.textAlignment = .center
label.font = .systemFont(ofSize: 20, weight: .medium)
return label
}()
private let iconImageView: UIImageView = {
let icon = UIImageView()
icon.translatesAutoresizingMaskIntoConstraints = false
icon.image = UIImage(systemName: "globe.americas")
icon.contentMode = .scaleAspectFit
return icon
}()
private let titleContainerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .secondarySystemBackground
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.backgroundColor = .tertiarySystemBackground
contentView.layer.cornerRadius = 8
contentView.layer.masksToBounds = true
contentView.addSubviews(titleContainerView, valueLabel, iconImageView)
titleContainerView.addSubviews(titleLabel)
setUpConstraints()
}
required init?(coder: NSCoder) {
fatalError()
}
private func setUpConstraints() {
NSLayoutConstraint.activate([
titleContainerView.leftAnchor.constraint(equalTo: contentView.leftAnchor),
titleContainerView.rightAnchor.constraint(equalTo: contentView.rightAnchor ),
titleContainerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
titleContainerView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.33),
titleLabel.leftAnchor.constraint(equalTo: titleContainerView.leftAnchor),
titleLabel.rightAnchor.constraint(equalTo: titleContainerView.rightAnchor),
titleLabel.topAnchor.constraint(equalTo: titleContainerView.topAnchor),
titleLabel.bottomAnchor.constraint(equalTo: titleContainerView.bottomAnchor),
iconImageView.heightAnchor.constraint(equalToConstant: 30),
iconImageView.widthAnchor.constraint(equalToConstant: 30),
iconImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 20),
iconImageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 36),
valueLabel.leftAnchor.constraint(equalTo: iconImageView.rightAnchor, constant: 10),
valueLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -10),
valueLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 36),
valueLabel.heightAnchor.constraint(equalToConstant: 30),
])
}
override func prepareForReuse() {
super.prepareForReuse()
valueLabel.text = nil
titleLabel.text = nil
iconImageView.image = nil
}
public func configure(with viewModel: RMCharacterInfoCollectionViewCellViewModel) {
}
}
'Swift > Rick & Morty' 카테고리의 다른 글
[iOS] Rick&Morty - #20 Fetch Episodes (0) | 2023.03.20 |
---|---|
[iOS] Rick&Morty - #19 Character Info ViewModel (0) | 2023.03.18 |
[iOS] Rick&Morty - #17 Character Photo Cell (0) | 2023.03.15 |
[iOS] Rick&Morty - #16 Character Detail ViewModels (0) | 2023.03.14 |
[iOS] Rick&Morty - #15 CollectionView Layouts (0) | 2023.03.13 |