[iOS] μ½λλ‘ UI ꡬμ±νκΈ° - Then λΌμ΄λΈλ¬λ¦¬ do λ©μλ μ΄μ©
μμ λ€μ μ½λ λ² μ΄μ€λ‘ UIλ₯Ό μ§κ³ , νλ‘κ·Έλλ°μ νκ³ μλ€. μνΈ μΈλ―Έλ ν λ μ€ν 리보λλ₯Ό λ무 λ§μ΄ λ΄€λμ§ μ μ λ©λ¦¬ ν λκ° λ κ² κ°λ€λ μκ°μ μ€μ€λ‘ νλ€... μΆ©λΆν λ§μ΄ λ΄€μ΄...π μ¬μ€ μ½λ λ² μ΄μ€λ‘ UIλ₯Ό μ§€ λ νμ κ³ λ―Όμ νλ λΆλΆμ μ½λκ° λ무 κΈΈμ΄μ§κ³ κ°λ μ±μ΄ μ’μ§ μλ€λ κ²μ΄μλ€.
μμΉ κ³Όμ μΈ Diary μ±μ λ§λ€λ©΄μ λ¬Έλ λ μκ°μ΄ λ€μ΄μ μ€νμΌμ μ’ λ°κΎΈλ €κ³ μλν΄λ³΄μλ€.
Then (Syntax Sugar Library)λ₯Ό μ¬μ©νλ€λ κ°μ νμ UI μ§λ μ€νμΌμ ν λ² μ΄ν΄λ³΄μ.
1. μΈμ€ν΄μ€ μμ± μμ ν΄λ‘μ λ‘ μμ±κΉμ§ μ§μ ν΄μ£Όλ λ°©λ²
UI κ°μ²΄κ° λͺ κ° μλ€λ©΄ μ€νλ € κΉλνκ³ κ°λ μ±λ μ’μ κ² κ°λ€.
let blackView = UIView().then {
$0.backgroundColor = .black
}
let nameLabel = UILabel().then {
$0.text = "text"
$0.textColor = .blue
$0.backgroundColor = .red
}
override func viewDidLoad() {
super.viewDidLoad()
configureLayout()
}
private func configureLayout() {
[blackView, nameLabel].forEach {
view.addSubview($0)
}
blackView.snp.makeConstraints {
$0.width.height.equalTo(200)
$0.top.equalTo(view.safeAreaLayoutGuide)
$0.centerX.equalTo(view)
}
nameLabel.snp.makeConstraints {
$0.edges.equalTo(blackView).inset(50)
}
}
2. μμ±μ λ©μλμμ μ§μ ν΄μ£Όλ λ°©λ²
μμ±μ κ°μ²΄ λ³λ‘ λ©μλλ₯Ό λλμ΄ μ§μ νλ λ°©λ²(- λ€μ΄λ° νλ κ²λ 머리 μνλ€ -)λ μκ³ , μμμλ configureAttributes λ©μλ μμ μμ± μ€μ νλ μ½λλ₯Ό μ λΆ λλ €λ°μλ€. λ²μ¨ λ³λ‘μΈ κ² κ°λ€. μμ± κ΅¬λΆλ μ λκ³ κ°λ μ±μ΄ λ³λ‘ μ’μ§ μλ€. (UI μμκ° λ λ§μμ§λ μν©μ μκ°ν΄λ³΄μ.)
let blackView = UIView()
let nameLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
configureAttributes()
configureLayout()
}
private func configureAttributes() {
blackView.backgroundColor = .red
nameLabel.text = "text"
nameLabel.textColor = .blue
nameLabel.backgroundColor = .red
}
private func configureLayout() {
[blackView, nameLabel].forEach {
view.addSubview($0)
}
blackView.snp.makeConstraints {
$0.width.height.equalTo(200)
$0.top.equalTo(view.safeAreaLayoutGuide)
$0.centerX.equalTo(view)
}
nameLabel.snp.makeConstraints {
$0.edges.equalTo(blackView).inset(50)
}
}
μμμ μ΄ν΄λ³Έ λ κ°μ§μ μ€νμΌμ μ΄λ νκ°? μ¬μ€ μ΄λ€ λ°©λ²μ μ¬μ©ν κ²μΈμ§λ μ·¨ν₯ μ°¨μ΄μΈ κ² κ°λ€. λ§μ λ ν¬μ§ν°λ¦¬λ₯Ό μ΄ν΄λ³΄λ©΄ μ μλ₯Ό λ§μ΄ μ¬μ©νλ κ² κ°μλ° UI μΈμ€ν΄μ€ μκ° μ‘°κΈμ΄λΌλ λ§μμ§λ©΄ μλΉν κ°λ μ±μ΄ λ³λ‘λΌκ³ λκ»΄μ§λ€. (κ°μΈμ μΈ μκ°) - RootViewλ§ λ―μ΄μ λ°λ‘ UIλ₯Ό ꡬμ±νλ λ°©μμ μ μ©νλ€κ³ νλλΌλ κ°μ λ¬Έμ μ μ κ·Έλλ‘ μλ€. λλ UI μΈμ€ν΄μ€κ° μ κ°μ§λ°νμΌλ©΄ μ’κ² μ΄μ νμμ λ°©λ²μ Then λΌμ΄λΈλ¬λ¦¬μ do λ©μλλ₯Ό μ΄μ©ν΄λ³΄κΈ°λ‘ νλ€. (맨λ Then λ©μλλ§ μ°λ€λ³΄λκΉ λ€λ₯Έ λ©μλκ° μλμ§λ λͺ°λλ€.)
λ€μκ³Ό κ°μ λ μ΄μμμ ꡬμ±νλ€κ³ κ°μ νκ³ λ³΄λλ‘ νμ.

Then λΌμ΄λΈλ¬λ¦¬μ do λ©μλ μ΄μ©
UI νλ‘νΌν°μ μΈμ€ν΄μ€λ₯Ό λ§λ€ λ μμ±κΉμ§ ν λ²μ μ§μ ν΄μ€¬μλλ° μ½λκ° μ’ μ§μ λΆν΄μ§κ³ , UI μμλ§ λΆλ¦¬μμΌμ λ³΄κ³ μΆλ€λ μκ°μ΄ κ³μ λ€μμλ€. κ·Έλ λ€κ³ μμ±μ νλμ λ©μλ μμμ μ μ μ΄μ£Όμλ κ·Έκ²λ κ°λ μ±μ΄ μ’μ§ μμλ€. Then λΌμ΄λΈλ¬λ¦¬μ do λ©μλ μ΄μ©ν΄λ³΄κΈ°λ‘ νλ€.
μ΄λ κ² μμ±νκ³ λλ νμ(μμ±μ λ©μλμμ μ§μ ν΄μ£Όλ λ°©λ²)μ λ¬Έμ μ μ΄μλ κ°λ μ±μ΄ μ μ’λ€λ κ²μ΄ μ’ ν΄κ²°λ λ― νλ€. κ·Έλ¦¬κ³ μνλ κ²μ²λΌ UI μΈμ€ν΄μ€λ₯Ό κ°μ§λ°ν λ³Ό μ μκ² λμλ€.
final class PhotoSearchView: BaseView {
lazy var searchBar = UISearchBar()
lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewLayout())
override func configureAttributes() {
searchBar.do {
$0.placeholder = "μ¬μ§μ κ²μν΄λ³΄μΈμ."
$0.searchTextField.font = .systemFont(ofSize: 14)
$0.searchTextField.backgroundColor = .clear
}
collectionView.do {
$0.register(PhotoCell.self, forCellWithReuseIdentifier: PhotoCell.reuseIdentifier)
let layout = UICollectionViewFlowLayout()
let spacing = 8.0
let cellWidth = (UIScreen.main.bounds.width - spacing * 2) / 3
layout.itemSize = CGSize(width: cellWidth, height: cellWidth)
layout.minimumInteritemSpacing = spacing
$0.collectionViewLayout = layout
}
}
...
}
λ§λ¬΄λ¦¬
μ€λ μμ±ν λ΄μ©μ μ λ§ κ°μΈμ μΈ μ·¨ν₯, μκ°μ΄κΈ° λλ¬Έμ μ°Έκ³ λ§ νμΌλ©΄ μ’κ² λ€. μ½λ μμ± μ€νμΌμ κ°μΈ μ·¨ν₯μ΄λκΉ!