반응형

현재 UIViewController이 present를 이용해서 Modal방식인지 아니면 push 된 것인지 확인하기 위해서는 다음과 같이 extension을 사용하면 편리합니다.

extension UIViewController {
    /// 현재 뷰 컨트롤러가 Modal (presented) 여부 (true = presented, false = pushed)
    var isModal: Bool {
        if let index = navigationController?.viewControllers.firstIndex(of: self), index > 0 {
            return false
        } else if presentingViewController != nil {
            return true
        } else if navigationController?.presentingViewController?.presentedViewController == navigationController {
            return true
        } else if tabBarController?.presentingViewController is UITabBarController {
            return true
        } else {
            return false
        }
    }
}

 

이를 활용해서 화면을 닫을때 다음과 같이 사용하면 됩니다.

func close() {
    if isModal {
        dismiss(animated: true)
    } else {
        navigationController?.popViewController(animated: true)
    }
}
반응형

'iOS > Tip' 카테고리의 다른 글

UIStackView 하위 뷰 제거하기  (0) 2023.05.08
UIPageViewController Scroll 막기  (0) 2023.05.08
로그 Print하기 (Traceable)  (0) 2023.05.08
WKWebView - 캐쉬 삭제하기  (0) 2022.12.14
UITextField AutoFill 무시하기  (0) 2022.12.08
화면 터치시 키보드 내리기  (0) 2022.12.08
WKWebView - UserAgent 추가  (0) 2022.12.08
WKWebView - href link 처리하기  (0) 2022.12.08
Posted by 까칠코더
,