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

后端 未结 14 2189
一生所求
一生所求 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 07:51

    Petronella's answer does not work if self.navigationController is modally presented but self is not equal to self.navigationController.viewControllers[0], in that case self is pushed.

    Here is how you could fix the problem.

    return self.presentingViewController.presentedViewController == self
                || (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController && self == self.navigationController.viewControllers[0])
                || [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
    

    And in Swift:

    return self.presentingViewController?.presentedViewController == self
            || (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController && self.navigationController?.viewControllers[0] == self)
            || self.tabBarController?.presentingViewController is UITabBarController
    

提交回复
热议问题