๊ณต๋ถํ ๋ด์ฉ์ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
๋: ๋ด๊ฐ ์ดํดํ ๋ด์ฉ
layout subviews ๊ด๋ จ ๋ฉ์๋
- layout subviews๊ด๋ จ ๋ฉ์๋๋?
- layout subViews ๊ด๋ จ ๋ฉ์๋ ์ข ๋ฅ
1. layout subviews๊ด๋ จ ๋ฉ์๋๋?
ViewController๊ด๋ จ ๋ฉ์๋์ธ viewDidLoad, ViewWillAppear, ViewDidAppear ๋ฉ์๋๊ฐ ๋ทฐ๊ฐ ํ๋ฉด์ ๋ณด์ผ ๋ ํธ์ถ๋๋ ๊ฒ์ฒ๋ผ, ๋ ์ด์์์ด ํ๋ฉด์ ๋ณด์ผ ๋ ํธ์ถ๋๋ ๋ฉ์๋์ด๋ค.
ํธ์ถ ์์(๋ ์ด์์ ๊ฒฐ์ ์์)
viewWillLayoutSubviews() -> layoutSubviews() -> viewDidLayoutSubviews()
2. layout subViews ๊ด๋ จ ๋ฉ์๋ ์ข ๋ฅ
2.1 viewWillLayoutSubviews()
๋ ์ด์์ ๋ฐฐ์น ์ ํธ์ถ
viewWillLayoutSubviews()
Called to notify the view controller that its view is about to layout its subviews.
When a view's bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.
๋ทฐ๊ฐ ํ์ ๋ทฐ๋ฅผ ๋ฐฐ์นํ๋ ค ํ ๋, ์ด๋ฅผ ๋ทฐ ์ปจํธ๋กค๋ฌ์๊ฒ ์๋ ค์ฃผ๊ธฐ ์ํด ํธ์ถ๋๋ค.
๋ทฐ์ ๊ฒฝ๊ณ๊ฐ ๋ณ๊ฒฝ๋ ๋, ๋ทฐ๋ ํ์ ๋ทฐ์ ์์น๋ฅผ ์กฐ์ ํ๋ค. ๋ทฐ ์ปจํธ๋กค๋ฌ๋ ๋ทฐ๊ฐ ํ์ ๋ทฐ๋ฅผ ๋ฐฐ์นํ๊ธฐ ์ ์ ์ด ๋ฉ์๋๊ฐ ์ด๋ ํ ์ค์ ์ ๋ณ๊ฒฝํ๋๋ก ์ฌ์ ์(override)ํ ์ ์๋ค. ๊ธฐ๋ณธ ์ด ๋ฉ์๋์ ๊ตฌํ์ ์๋ฌด๊ฒ๋ ์ํํ์ง ์๋ ๊ฒ์ด๋ค.
๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ ์ฌ์ฉํ ์ ์๋ค.
- ๋ทฐ ์ถ๊ฐ ํน์ ์ ๊ฑฐ
- ๋ทฐ ํฌ๊ธฐ ๋๋ ์์น ์ ๋ฐ์ดํธ
- ๋ ์ด์์ constraint ์ ๋ฐ์ดํธ
- ๋ทฐ์ ๊ด๋ จ๋ ๊ธฐํ ํ๋กํผํฐ ์ ๋ฐ์ดํธ
2.2 layoutSubviews()
๋ ์ด์์ ๋ฐฐ์น ์ ํธ์ถ
layoutSubviews()
Lays out subviews.
the default implementation uses any constraints you have set to determine the size and position of any subviews.
Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded() method.
ํ์ ๋ทฐ๋ฅผ ๋ฐฐ์นํ๋ค.
๊ธฐ๋ณธ ๊ตฌํ์ ํ์ ๋ทฐ์ ์์น์ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ ํ๊ธฐ ์ํด ์ค์ ํ constraint(์ ์ฝ์กฐ๊ฑด)์ ์ฌ์ฉํ๋ค. -> ๋: ์คํ ๋ ์ด์์
๋ ์ ํํ ํ์ ๋ทฐ ๋ฐฐ์น๊ฐ ํ์ํ ๋, ์์ ํด๋์ค๋ ์ด ๋ฉ์๋๋ฅผ ์ฌ์ ์(override)ํ ์ ์๋ค. ์ด ๋ฉ์๋๋ ๋ด๊ฐ ์ํ๋ ์๋ ํฌ๊ธฐ ์กฐ์ (autoresizing) ๋ฐ ์ ์ฝ ์กฐ๊ฑด ๊ธฐ๋ฐ(constraint-based behaviors) ๋์์ด ์ ๊ณต๋์ง ์๋ ๊ฒฝ์ฐ์ ํํด ์ฌ์ ์(override)ํ ์ ์๋ค. ์ฌ์ ์๋ฅผ ํตํด ํ์๋ทฐ์ ํ๋ ์์ ์ง์ ์ค์ ํ ์ ์๋ค.
+ ๋ทฐ์ ํฌ๊ธฐ๊ฐ ๋ณ๊ฒฝ๋๋ฉด ์ฐ์ ํ์ ๋ทฐ๋ค์ autoresiziong ๋์์ ์ ์ฉํ๋๋ฐ, ๋ณ๊ฒฝ์ฌํญ์ ๋ฐ์ํ๊ธฐ ์ํด์ layoutSubviews() ๋ฉ์๋๋ฅผ ํธ์ถ(์ด ๋ฉ์๋ ๋ํ ํ์ ๋ทฐ์์ ์ฐ์์ ์ผ๋ก ํธ์ถ๋๋ค.) -> ๋: ์์ง ์ ๋๋ก ์ดํด๋์ง ์์
2.3 viewDidLayoutSubviews()
๋ ์ด์์ ๋ฐฐ์น ํ ํธ์ถ
viewDidLayoutSubviews()
Called to notify the view controller that its view has just laid out its subviews
When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.
Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.
๋ทฐ๊ฐ ํ์ ๋ทฐ๋ฅผ ๋ฐฉ๊ธ ๋ฐฐ์นํ์ ๋, ์ด๋ฅผ ๋ทฐ ์ปจํธ๋กค๋ฌ์๊ฒ ์๋ฆฌ๊ธฐ ์ํด ํธ์ถํ๋ค.
view controller์ ๋ทฐ์ ๋ํ ๊ฒฝ๊ณ๊ฐ ๋ณ๊ฒฝ๋ ๋, ๋ทฐ๊ฐ ํ์ ๋ทฐ์ ์์น๋ฅผ ์กฐ์ ํ ํ ์์คํ ์ด ์ด ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค. ๊ทธ๋ฌ๋ ํธ์ถ๋ ์ด ๋ฉ์๋๋ ๋ทฐ์ ํ์๋ทฐ์ ๊ฐ๊ฐ์ ๋ ์ด์์์ด ์กฐ์ ๋์์์ ๋ํ๋ด์ง ์๋๋ค. ๊ฐ ํ์ ๋ทฐ๋ ์์ ์ ๋ ์ด์์์ ์กฐ์ ํ ์ฑ ์์ด ์๋ค. view controller๋ ๋ทฐ๊ฐ ํ์ ๋ทฐ๋ฅผ ๋ฐฐ์นํ ํ ์ด๋ ํ ์ค์ ์ ๋ณ๊ฒฝํ๊ธฐ ์ํด ์ด ๋ฉ์๋๋ฅผ ์ฌ์ ์(override)ํ ์ ์๋ค. ์ด ๋ฉ์๋์ ๊ธฐ๋ณธ ๊ตฌํ์๋ ์๋ฌด๊ฒ๋ ์ํํ์ง ์๋ ๊ฒ์ด๋ค.
๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์ ์ฌ์ฉํ ์ ์๋ค.
- ๋ค๋ฅธ ๋ทฐ๋ค์ content ์ ๋ฐ์ดํธ
- ๋ทฐ ํฌ๊ธฐ๋ ์์น ์ต์ข ์ ์ผ๋ก ์กฐ์
- ํ ์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ reload ํ๋ค.
๋->
๋ทฐ ๋ ์ด์์ ๊ฒฐ์ ํ ํธ์ถ๋๋ ๋ฉ์๋
๋ทฐ : View Controller ๋ฐ๋ก ์๋์ View
ํ์ ๋ทฐ: ๊ทธ View ์๋์ ๋ฐฐ์น๋๋ ๋ ์ด๋ธ, ๋ฒํผ ๋ฑ์ ๊ฐ์ฒด

๊ณต๋ถ ์๋ฃ
1. viewDidLayoutSubviews()์์ tab bar ๋ ์ด์์ ์ค์ ํ๋ ์์
https://stackoverflow.com/questions/58078821/change-tabbar-height-in-swift-5-for-ios-13
2. layout subview ๋ฉ์๋
https://ios-development.tistory.com/195
3. Apple developer ๋ฌธ์
https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews/