Is it possible to determine whether ViewController is presented as Modal?

后端 未结 14 2022
一生所求
一生所求 2020-12-04 07:13

Is it possible to check inside ViewController class that it is presented as modal view controller?

相关标签:
14条回答
  • 2020-12-04 08:03

    In Swift:

    func isUIViewControllerPresentedAsModal() -> Bool {
        if((self.presentingViewController) != nil) {
            return true
        }
    
        if(self.presentingViewController?.presentedViewController == self) {
            return true
        }
    
        if(self.navigationController?.presentingViewController?.presentedViewController == self.navigationController) {
            return true
        }
    
        if((self.tabBarController?.presentingViewController?.isKindOfClass(UITabBarController)) != nil) {
            return true
        }
    
        return false
    }
    
    0 讨论(0)
  • 2020-12-04 08:04

    Since modalViewController has been deprecated in iOS 6, here's a version that works for iOS 5+ and that compiles without warnings.

    Objective-C:

    - (BOOL)isModal {
        return self.presentingViewController.presentedViewController == self
          || (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController)
          || [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
    }
    

    Swift:

    var isModal: Bool {
        return self.presentingViewController?.presentedViewController == self
            || (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController)
            || self.tabBarController?.presentingViewController is UITabBarController
    }
    

    Hat tip to Felipe's answer.

    0 讨论(0)
提交回复
热议问题