Component ๐ญ
์ ํ๋จ์ ๋ฒํผ์ด ๋ง์ ๋ทฐ์์ ์ฌ์ฌ์ฉ๋๊ณ ์์ด์ ์ปดํฌ๋ํธํ ํ์ต๋๋ค
์ด๊ธฐํํ ๋ setLayout์์ width์ height๋ฅผ ์ก์์คฌ๊ตฌ, centerX๋ง ์ก์์ฃผ๋ฉด ๋ฐ๋ก ์ ์ฉ๋ ์ ์๋๋ก ํ์ต๋๋ค.
๊ทธ ์ธ์ ๋ฒํผ UI์ title, ํ์ฑ·๋นํ์ฑํ ์ํ ๋ฑ์ setUI(.type), setTitle, setAble ๋ฑ์ ํจ์๋ก ์ค์ ํ ์ ์๋๋ก ํ์ต๋๋ค.
๋ฌธ์ ๐จ
์ ๋ ์ฝ๋๋ฒ ์ด์ค๋ก ๋ทฐ๋ฅผ ์งฐ๊ธฐ ๋๋ฌธ์ BottomButton ์ด๋ผ๋ ํด๋์ค๋ช ์ผ๋ก ์ฌ์ฉํ๋๋ฐ
์คํ ๋ฆฌ๋ณด๋๋ฅผ ์ฌ์ฉํ๋ ํ์์ ์ด๊ฑธ ์ด๋ป๊ฒ ์ฌ์ฉํ๋ ํ๋ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค..
ํด๊ฒฐ ๋ฐฉ๋ฒ๐ก
๋ฐฉ๋ฒ์ ์๊ฐ๋ณด๋ค ๊ต์ฅํ ๊ฐ๋จํ์ด๋๋ค !
๋ณดํต ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ๋ฃ์ด์ฃผ๋ required init์๋ ์ด๊ธฐํ ํจ์๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋ฉ๋๋น
class BottomButton: UIButton {
// MARK: - Initialize
override init(frame: CGRect) {
super.init(frame: frame)
setLayout()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setLayout()
}
// ...
}
initโ
init(frame:)
์ง์ ๋ ํ๋ ์ ์ฌ๊ฐํ์ผ๋ก ์๋ก ํ๋๋ ๋ทฐ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๊ณ ๋ฐํ
- UIVIew๋ UIViewController์ ์๋ธํด๋์ฑํ๋ฉด ์์ฑ๋๋ ์ด๋์ ๋ผ์ด์
- Frame rectangle์ ๊ฐ์ง๊ณ ๊ตฌํํ๊ณ ์ ํ๋ ๋ทฐ์ ์ค์ฌ๊ณผ ๊ฒฝ๊ณ์ ์ ์ง์
- ์ค๋ณด, xib, nib ๊ฐ์ ์ธํฐํ์ด์ค ๋น๋๊ฐ ์๋ ์ฝ๋๋ฒ ์ด์ค๋ก UIView Class์ ๋ทฐ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋ ์ฌ์ฉ
๐ฅ ์๋ก์ด ๋ทฐ ๊ฐ์ฒด๋ ์ฌ์ฉ๋๊ธฐ ์ ์ ๋ทฐ ๊ณ์ธต์ ์ฌ๋ผ๊ฐ์ผ ํฉ๋๋ค. ํด๋น ๋ฉ์๋๋ ์ฝ๋๋ก ๋ทฐ๋ฅผ ์์ฑํ ๊ฒฝ์ฐ UIView ํด๋์ค๋ฅผ ์ด๊ธฐํํด์ฃผ๋๋ก ์ค๊ณ๋์ด ์์ต๋๋ค. Subclass๋ค์ ํด๋น ํด์๋๋ฅผ overrideํด์ ์ด๊ธฐํํ ์ ์์ผ๋ฉฐ, ๋ฐ๋์ super์ ์ฌ์ฉํด์ค์ผ ํฉ๋๋ค! ์ธํฐํ์ด์ค ๋น๋๋ฅผ ์ฌ์ฉํ์ฌ ์ธํฐํ์ด์ค๋ฅผ ์ค๊ณํ ๊ฒฝ์ฐ, nib ํ์ผ๋ก๋ถํฐ ๋ทฐ๋ฅผ ๋ถ๋ฌ์ฌ ๋ ํด๋น ๋ฉ์๋๋ ๋ถ๋ฆฌ์ง ์์ต๋๋ค. nib ํ์ผ์ ๊ฐ์ฒด๋ ์ฌ๊ตฌ์ฑ๋์ด์ผ ํ๋ฉฐ init(coder:) ๋ฉ์๋๋ฅผ ํตํด ์ด๊ธฐํ๋ฉ๋๋ค.
๐ init(frame:)
init(coder:)
storyboard๋ xib์ ๊ฐ์ ์ธํฐํ์ด์ค ๋น๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ,
์ด๋ ์ฝ๋๊ฐ ์๋๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ๋ฌ๊ฐ ์ธ์ํ ์ ์๋๋ก ์ฝ๋๋ก ๋ณํํด์ฃผ๋ unarchiving ๊ณผ์ ์ด ํ์
์ด ๊ณผ์ ์ init(coder:) ์์ ์ฑ ๋ด view ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฌ์์ค๋๋ค
required init
๋ชจ๋ ์์ ํด๋์ค์์ ๋ฐ๋์ ๊ตฌํํด์ผ ํ๋ ์ด๋์ ๋ผ์ด์
์ฐธ๊ณ ์๋ฃ ๐
https://sweetdev.tistory.com/366
https://velog.io/@inwoodev/iOS-initframe-initcoder
https://leeari95.tistory.com/63?category=961651
https://developer.apple.com/documentation/uikit/uiview/1622488-init
https://developer.apple.com/documentation/foundation/nscoding/1416145-init
'iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS] ํธ์์๋ฆผ ๋ฐ์์ ์ฒ๋ฆฌํ๊ธฐ (0) | 2022.10.12 |
---|---|
[iOS] Compositional Layout ๐โโ๏ธ (0) | 2022.09.05 |
[iOS] RxDataSource ๐ก (0) | 2022.05.17 |
[iOS] Haptic ์ฌ์ฉ๋ฒ๐ (0) | 2022.04.23 |
[iOS] Diffable Datasourceโ (0) | 2022.03.18 |