How to check if a view controller is presented modally or pushed on a navigation stack? How to check if a view controller is presented modally or pushed on a navigation stack? ios ios

How to check if a view controller is presented modally or pushed on a navigation stack?


Take with a grain of salt, didn't test.

- (BOOL)isModal {     if([self presentingViewController])         return YES;     if([[[self navigationController] presentingViewController] presentedViewController] == [self navigationController])         return YES;     if([[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]])         return YES;    return NO; }


In Swift:

Add a flag to test if it's a modal by the class type:

// MARK: - UIViewController implementationextension UIViewController {    var isModal: Bool {        let presentingIsModal = presentingViewController != nil        let presentingIsNavigation = navigationController?.presentingViewController?.presentedViewController == navigationController        let presentingIsTabBar = tabBarController?.presentingViewController is UITabBarController        return presentingIsModal || presentingIsNavigation || presentingIsTabBar    }}


You overlooked one method: isBeingPresented.

isBeingPresented is true when the view controller is being presented and false when being pushed.

- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated];    if ([self isBeingPresented]) {        // being presented    } else if ([self isMovingToParentViewController]) {        // being pushed    } else {        // simply showing again because another VC was dismissed    }}