์์น์์ ํ๋ฐ๊ธฐ ํ๋ก์ ํธ๋ฅผ ๊ฐ๋ฐํ ๋น์ CustomView๋ฅผ ๋ง์ด ์ฌ์ฉํ๊ฒ ๋์ด์ ์ ๋ฆฌํ๊ฒ ๋์๋ค.
์ฃผ๋ก ์ฝ๋๋ฒ ์ด์ค๋ก UI๋ฅผ ๊ทธ๋ ค์๊ธฐ ๋๋ฌธ์ ํด๋น ๊ธ์์ ์ฝ๋ ๋ฒ ์ด์ค + SnapKit ๊ธฐ๋ฐ์ผ๋ก customView๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃฐ ์์ ์ด๋ค.
์ปค์คํ ๋ทฐ๋ฅผ ์ ์ฌ์ฉํ ๊นโ
์ฐ์ ๋ด๊ฐ CustomView๋ฅผ ์ฌ์ฉํ๋ ๊ฐ์ฅ ํฐ ์ด์ ๋ ์ฌ์ฌ์ฉ์ฑ ๋๋ฌธ์ด์๋ค.
์ ์บก์ณ ์ด๋ฏธ์ง์ฒ๋ผ, ๋ฐ๋ณต๋๋ UI ์์๊ฐ ๋ง์ ํ๋ฉด์ ๊ทธ๋ฆด ๋ ๋งค๋ฒ UI์ ๊ด๋ จ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๊ต์ฅํ ๋นํจ์จ์ ์ผ ์ ๋ฐ์ ์๋ค.
ํฐํธ๋ฅผ ๋ฐ๊ฟ์ผํ๋ค๋ฉด?
ํฌ์ธํธ ์ปฌ๋ฌ๋ฅผ ๋ฐ๊ฟ์ผํ๋ค๋ฉด?
๋ฒํผ์ ์ฝ๋ ๋ ๋์ด์ค๋ฅผ ๋ณ๊ฒฝํด์ผํ๋ค๋ฉด?
๋์ผํ ๊ตฌ์ฑ์ ์ฝ๋๋ฅผ ์ผ์ผ์ด ์ฐพ์์ ์์ ํ๋ค๋ฉด, ๋ฐ๋ณต๋๋ ๋ทฐ์ ์๊ฐ ๋ง์ ์๋ก ์์ ์ ๊ตด๋ ์ ๋น ์ง ์ ๋ฐ์ ์์ผ๋๊น.
๋ํ, ๋ถ๋ถ์ ์ผ๋ก ๋ค๋ฅธ ๋ทฐ์์๋ ๊ฐ์ UI๋ฅผ ๊ทธ๋ ค์ผํ ๋๋ ์๊ธฐ ๋๋ฌธ์ ์ด๋ด ๋ ํด๋น UI ์ฝ๋๋ฅผ ์ฌ์ฌ์ฉํ๋ ค๋ฉด CustomView ์ฌ์ฉ์ด ์ ์ ํ๋ค.
โ๏ธ ์ปค์คํ ๋ทฐ๋ฅผ ํ์ฉํ๊ธฐ
1. ๋ฒํผ ์ฌ์ฌ์ฉ
import UIKit
class NextButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setting()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setting() {
setTitleColor(.white, for: .normal)
backgroundColor = ConstantColor.inActiveBrand
layer.cornerRadius = 8
titleLabel?.font = ConstantTypo.title2
}
}
ํ์๊ฐ์
์ ํ ๋, ๋ค์ ๋ฒํผ์ ๋ง๋ค ๋ UI๊ฐ ๋์ผ ํ๊ธฐ ๋๋ฌธ์ ์ปค์คํ
๋ทฐ๋ก ๊ด๋ฆฌํ๋ค.
init(frame: CGRect) ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ์๋์ผ๋ก required init?(coder: NSCoder) ์ฝ๋๋ฅผ ์์ฑํ๋๋ก ์ปดํ์ผ ์์ ์ ์๋ด๊ฐ ๋ฌ๋ค.
๋ ๊ฐ์ง๋ฅผ ๊ฐ๋จํ ์ ๋ฆฌํ์๋ฉด,
init(frame:)์ ์ฝ๋๋ก ๋ทฐ๋ฅผ ์์ฑํ ๋์ ์์ฑ์์ด๊ณ ,
init(coder:)๋ ์คํ ๋ฆฌ๋ณด๋ ๊ธฐ๋ฐ์ผ๋ก ๋ทฐ๊ฐ ์์ฑ ๋ ๋์ ์์ฑ์์ด๋ค.
๋๋ ์ฝ๋๋ก ๋ทฐ๋ฅผ ๊ทธ๋ฆฌ๊ณ ์๊ธฐ ๋๋ฌธ์, init(frame:)์๋ง ๋ด๊ฐ ์ฌ์ฌ์ฉํ๊ณ ์ถ์ ๊ณ ์ UI ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค.
let nextButton: UIButton = {
let btn = NextButton()
btn.setTitle("์ํฌ์คํ์ด์ค ์์ฑ", for: .normal)
return btn
}()
์ปค์คํ
์ผ๋ก ๋ง๋ ๋ฒํผ์ ์ ์ฝ๋์ฒ๋ผ ๋ค๋ฅธ ํด๋์ค๋ก ๋ถ๋ฌ์ ์ ์ฉํ๋ฉด ๋๋ค.
2. ๋ทฐ ์ฌ์ฌ์ฉ
๋ทฐ ์์ฒด๊ฐ ๋ฐ๋ณต๋๋ UI๊ฐ ๋ง์๋ SLP ๋ทฐ๋ฅผ ๊ทธ๋ฆฌ๋ฉฐ CustomView ์ฌ์ฉ์ ํ์ฅํด๋ณผ ์ ์์๋ค.
๋๋ UI ์์ ํ๋ํ๋๋ฅผ CustomView๋ก ๋ง๋ค์ด ๊ด๋ฆฌ๋ฅผ ํด์์๋๋ฐ, ์ด ํ๋ก์ ํธ ์ดํ ๋ฐ๋ณต๋๋ view ์์ฒด๋ฅผ custom์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ฒ ๋์๋ค.
import UIKit
import SnapKit
final class ListView: UIView {
let customButton = {
let btn = UIButton()
return btn
}()
let iconImageView = {
let view = UIImageView()
view.image = ConstantIcon.hashThin
return view
}()
let titleLabel = {
let label = UILabel()
label.font = ConstantTypo.body
label.textColor = UIColor.textSecondary
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
configureViews()
setConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configureViews() {
addSubview(iconImageView)
addSubview(titleLabel)
addSubview(customButton)
}
func setConstraints() {
iconImageView.snp.makeConstraints { make in
make.leading.equalToSuperview().offset(14)
make.size.equalTo(18)
make.centerY.equalToSuperview()
}
titleLabel.snp.makeConstraints { make in
make.leading.equalTo(iconImageView.snp.trailing).offset(16)
make.height.equalTo(28)
make.width.equalTo(290)
make.centerY.equalTo(iconImageView)
}
customButton.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
}
customView ๋ด์์ ๊ธฐ๋ณธ ๋ ์ด์์์ ์ก์๋ ์ ์๋ค.
์ฐพ์๋ณด๋ frame์ ์ฌ์ฉํด์ ์ฌ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ ์๋๋ฐ, ์ผ๋จ ๋๋ snapKit ์ฌ์ฉ์ด ์ต์ํด์ ๋ด๊ฐ ํ๋ ๋ฐฉ์๋๋ก ์์ฑํ๋ค.
let guideView = {
let view = ListView()
view.iconImageView.image = ConstantIcon.helpCustom
view.titleLabel.text = "๋์๋ง"
return view
}()
์ปค์คํ
์ผ๋ก ๋ง๋ ListView๋ ์์ฒ๋ผ ์์ํด์ ํ์์ ๋ฐ๋ผ ๋ด๋ถ UI๋ฅผ ์์ ํด์ ์ฌ์ฉํ๋ฉด ๋๋ค.
โ๏ธ ๋ง๋ฌด๋ฆฌ: ์ปค์คํ ๋ทฐ ์ฌ์ฉ์ ์ฅ์
์ด๋ ๊ฒ ๋ฐ๋ณต๋๋ UI ์ฝ๋๋ฅผ ํด๋์ค๋ก ์ปค์คํ
๋ทฐ๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ๋ฉด view์์์ ์ฝ๋๊ฐ ํ์คํ ์ค์ด์๋ ๊ฑธ ๋ณผ ์ ์๋ค.
'๐ Dev > UIKit' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[UIKit] ์ฑ์ ์๋ช ์ฃผ๊ธฐ (0) | 2023.11.27 |
---|---|
[UIKit] Font ์ค์ ํ๊ธฐ (0) | 2023.11.02 |
[UIKit/Storyboard] ํค๋ณด๋ ๋ด๋ฆฌ๊ธฐ (0) | 2023.07.30 |
[UIKit/Storyboard] ํ ํ๋ก์ ํธ์์ ๋ค๋ฅธ ์คํ ๋ฆฌ๋ณด๋ ๋ณด๋ ๋ฒ (0) | 2023.07.30 |
[UIKit/Storyboard] ์์ธ๋ ํจ์ (@IBOutlet)๊ณผ ์ก์ ํจ์ (@IBAction) (0) | 2023.07.29 |