Project/HiddenGem
🤔 뷰 컨트롤러가 모달로 띄워졌는지 어떻게 알 수 있나?
밤새는 탐험가89
2025. 6. 2. 16:20
✅ 이 로직이 필요한 이유
UIViewController에는 isModal 같은 속성이 없어서,
push로 띄웠는지 / present로 띄웠는지 직접 판단해야 합니다.
var isModal: Bool {
if let navigationController = navigationController {
if navigationController.viewControllers.first != self {
return false
}
}
if presentingViewController != nil {
return true
}
if navigationController?.presentingViewController?.presentedViewController == navigationController {
return true
}
if tabBarController?.presentingViewController is UITabBarController {
return true
}
return false
}
✅ 각 조건 설명
if let navigationController = navigationController {
if navigationController.viewControllers.first != self {
return false
}
}
- 내가 navigation stack 안에 있는 VC인지 확인
- 내가 첫 번째가 아니라면 → push된 것 → 모달이 아님
if presentingViewController != nil {
return true
}
- 나를 띄운 VC가 있다면 → 모달로 띄워졌을 가능성 있음
- 이건 가장 일반적인 모달 판단 조건
if navigationController?.presentingViewController?.presentedViewController == navigationController {
return true
}
- 내가 navigationController에 감싸져 있는데,
- 그 navigationController 자체가 present된 것이면 → 모달
- 예: present(UINavigationController(rootViewController: self))
if tabBarController?.presentingViewController is UITabBarController {
return true
}
- 탭바 컨트롤러 전체가 present된 경우를 감지
- 예: present(MyTabBarController())
return false
- 그 외는 전부 false