Skip to content

Instantly share code, notes, and snippets.

@paulrehkugler
Last active December 1, 2015 17:25
Show Gist options
  • Save paulrehkugler/9749388 to your computer and use it in GitHub Desktop.
Save paulrehkugler/9749388 to your computer and use it in GitHub Desktop.
@interface UINavigationController(Completion)
- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion;
@end
#import "UINavigationController+Completion.h"
#import <objc/runtime.h>
@interface UINavigationController(Completion_Private)<UINavigationControllerDelegate>
@property (nonatomic, copy) void (^per_completionBlock)();
@property (nonatomic) id<UINavigationControllerDelegate> per_oldDelegate;
@end
@implementation UINavigationController(Completion)
- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion
{
self.per_oldDelegate = self.delegate;
self.delegate = self;
self.per_completionBlock = completion;
[self pushViewController:viewController animated:animated];
}
@end
@implementation UINavigationController(Completion_Private)
- (void) setPer_oldDelegate:(id<UINavigationControllerDelegate>)per_oldDelegate
{
objc_setAssociatedObject(self, @selector(per_oldDelegate), per_oldDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id<UINavigationControllerDelegate>) per_oldDelegate
{
return objc_getAssociatedObject(self, @selector(per_oldDelegate));
}
- (void) setPer_completionBlock:(void (^)())per_completionBlock
{
objc_setAssociatedObject(self, @selector(per_completionBlock), per_completionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)())per_completionBlock
{
return objc_getAssociatedObject(self, @selector(per_completionBlock));
}
- (void) navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.per_completionBlock)
{
self.per_completionBlock();
}
if ([self.per_oldDelegate respondsToSelector:@selector(navigationController:didShowViewController:animated:)])
{
[self.per_oldDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
}
self.delegate = self.per_oldDelegate;
}
- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self.per_oldDelegate respondsToSelector:@selector(navigationController:willShowViewController:animated:)])
{
[self.per_oldDelegate navigationController:navigationController willShowViewController:viewController animated:animated];
}
}
- (id<UIViewControllerInteractiveTransitioning>) navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
if ([self.per_oldDelegate respondsToSelector:@selector(navigationController:interactionControllerForAnimationController:)])
{
return [self.per_oldDelegate navigationController:navigationController interactionControllerForAnimationController:animationController];
}
return nil;
}
- (UIInterfaceOrientation) navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController
{
if ([self.per_oldDelegate respondsToSelector:@selector(navigationControllerPreferredInterfaceOrientationForPresentation:)])
{
return [self.per_oldDelegate navigationControllerPreferredInterfaceOrientationForPresentation:navigationController];
}
switch ([[UIDevice currentDevice] orientation])
{
case UIDeviceOrientationUnknown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationPortrait:
{
return UIInterfaceOrientationPortrait;
}
case UIDeviceOrientationLandscapeLeft:
{
return UIInterfaceOrientationLandscapeLeft;
}
case UIDeviceOrientationLandscapeRight:
{
return UIInterfaceOrientationLandscapeRight;
}
case UIDeviceOrientationPortraitUpsideDown:
{
return UIInterfaceOrientationPortraitUpsideDown;
}
}
}
- (NSUInteger) navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
{
if ([self.per_oldDelegate respondsToSelector:@selector(navigationControllerSupportedInterfaceOrientations:)])
{
return [self.per_oldDelegate navigationControllerSupportedInterfaceOrientations:navigationController];
}
return UIInterfaceOrientationMaskAll;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment