public
Last active

Checking whether ViewController is presented as Modal - iOS 5-

  • Download Gist
UIViewController+ModalCheck.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11
// UIViewController+ModalCheck.h
//
// Created by Felipe Sabino on 06/14/11
 
#import <UIKit/UIKit.h>
 
@interface UIViewController (ModalCheck)
 
-(BOOL)isPresentedAsModal;
 
@end
UIViewController+ModalCheck.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// UIViewController+ModalCheck.m
//
// Created by Felipe Sabino on 06/14/11
 
#import "UIViewController+ModalCheck.h"
 
@implementation UIViewController (ModalCheck)
 
-(BOOL)isPresentedAsModal {
 
BOOL isModal = ((self.parentViewController && self.parentViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
( self.navigationController && self.navigationController.parentViewController && self.navigationController.parentViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] parentViewController] isKindOfClass:[UITabBarController class]]);
 
//iOS 5+
if (!isModal && [self respondsToSelector:@selector(presentingViewController)]) {
 
isModal = ((self.presentingViewController && self.presentingViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
(self.navigationController && self.navigationController.presentingViewController && self.navigationController.presentingViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);
 
}
 
return isModal;
 
}
 
@end

This is deprecated for iOS 6+, please check here if that's your case

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.