How to use single storyboard uiviewcontroller for multiple subclass

great question – but unfortunately only a lame answer. I don’t believe that it is currently possible to do what you propose because there are no initializers in UIStoryboard that allow overriding the view controller associated with the storyboard as defined in the object details in the storyboard on initialization. It’s at initialization that all … Read more

Completion block for popViewController

I know an answer has been accepted over two years ago, however this answer is incomplete. There is no way to do what you’re wanting out-of-the-box This is technically correct because the UINavigationController API doesn’t offer any options for this. However by using the CoreAnimation framework it’s possible to add a completion block to the … Read more

How to lock orientation of one view controller to portrait mode only in Swift

Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers. This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews. Swift 3, … Read more

viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller

You can use the following. – (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; NSArray *viewControllers = self.navigationController.viewControllers; if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) { // View is disappearing because a new view controller was pushed onto the stack NSLog(@”New view controller was pushed”); } else if ([viewControllers indexOfObject:self] == NSNotFound) { // View is disappearing … Read more

How to get root view controller?

if you are trying to access the rootViewController you set in your appDelegate. try this: Objective-C YourViewController *rootController = (YourViewController*)[[(YourAppDelegate*) [[UIApplication sharedApplication]delegate] window] rootViewController]; Swift let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate let viewController = appDelegate.window!.rootViewController as YourViewController Swift 3 let appDelegate = UIApplication.shared.delegate as! AppDelegate let viewController = appDelegate.window!.rootViewController as! YourViewController Swift 4 & 4.2 … Read more

Animate change of view controllers without using navigation controller stack, subviews or modal controllers?

EDIT: New answer that works in any orientation. The original answer only works when the interface is in portrait orientation. This is b/c view transition animations that replace a view w/ a different view must occur with views at least a level below the first view added to the window (e.g. window.rootViewController.view.anotherView). I’ve implemented a … Read more

How do I Disable the swipe gesture of UIPageViewController?

The documented way to prevent the UIPageViewController from scrolling is to not assign the dataSource property. If you assign the data source it will move into ‘gesture-based’ navigation mode which is what you’re trying to prevent. Without a data source you manually provide view controllers when you want to with setViewControllers:direction:animated:completion method and it will … Read more

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; }

Display clearColor UIViewController over UIViewController

iOS8+ In iOS8+ you can now use the new modalPresentationStyle UIModalPresentationOverCurrentContext to present a view controller with a transparent background: MyModalViewController *modalViewController = [[MyModalViewController alloc] init]; modalViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext; [self presentViewController:modalViewController animated:YES completion:nil];

Fatal error: use of unimplemented initializer ‘init(coder:)’ for class

Issue This is caused by the absence of the initializer init?(coder aDecoder: NSCoder) on the target UIViewController. That method is required because instantiating a UIViewController from a UIStoryboard calls it. To see how we initialize a UIViewController from a UIStoryboard, please take a look here Why is this not a problem with Objective-C? Because Objective-C … Read more