iOS/설명
[iOS] viewWillAppear vs viewIsAppearing
Sky Titan
2025. 11. 2. 14:04
728x90
https://developer.apple.com/documentation/uikit/uiviewcontroller/4195485-viewisappearing
viewIsAppearing(_:) | Apple Developer Documentation
Notifies the view controller that the system is adding the view controller’s view to a view hierarchy.
developer.apple.com
viewIsAppearing
- viewController에게 시스템이 현재 view를 view hierarchy에 추가하고 있음을 알려주는 콜백함수
- viewWillAppear가 호출되고 난 이후에 불리는데, viewWillAppear와 다르게 view가 view hierarchy에 추가되고 superview에 layout된 이후에 호출된다.
- 따라서 정확한 view의 geometry 값들이 계산되고 난 이후에 호출되기 때문에 view의 위치값이나 사이즈를 활용한 UI 작업들을 정확히 수행할 수 있다.

콜백 시점의 상태 비교
| 상태 | viewWillAppear(_:) | viewIsAppearing(_:) |
| 애니메이션과 함께 추가할 수 있는 전환 코디네이터(Transition Coordinator) 사용 가능 | ✔️ | ✖️ |
| 뷰가 계층 구조(View Hierarchy) 에 추가됨 | ✖️ | ✔️ |
| 뷰 컨트롤러 및 뷰의 트레잇 컬렉션(Trait Collection) 이 업데이트됨 | ✖️ | ✔️ |
| 뷰의 기하(Geometry) — 크기, 세이프 영역(safe area) 등 — 이 정확함 | ✖️ | ✔️ |
그리고 viewWillLayoutSubviews나 viewDidLayoutSubviews의 경우엔 viewController에 있는 view의 layoutSubviews 함수가 호출될 때마다 여러 번 호출되는 함수들이지만 viewIsAppearing은 viewController의 view가 최초로 layout된 직후에 한 번만 호출된다는 차이점이 있다.
그렇기 때문에 반복적이 아닌, viewController 로드 후 view의 geometry를 이용해서 최초로 한 번만 수행되어야하는 작업들이 필요할 때 사용하면 좋다.
(기존에는 대신 viewWillLayoutSubviews와 flag값을 활용해서 수행해야 했음)
728x90