Skip to content

Commit

Permalink
Re-implement UINavigationControllerDelegate;
Browse files Browse the repository at this point in the history
  • Loading branch information
cuzv committed Jan 5, 2018
1 parent ddc3433 commit 0685e9a
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 132 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@

### 0.4.0

- `RRNavigationControllerDelegate`
- `RRNavigationControllerDelegate`

### 0.4.1

- Remove `RRNavigationControllerDelegate`
- Re-implement `UINavigationControllerDelegate`
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://github.com/cuzv/RRNavigationBar/blob/master/LICENSE)
[![CocoaPods Compatible](https://img.shields.io/badge/CocoaPods-v0.3.3-green.svg)](https://github.com/CocoaPods/CocoaPods)
[![CocoaPods Compatible](https://img.shields.io/badge/CocoaPods-v0.4.1-green.svg)](https://github.com/CocoaPods/CocoaPods)
[![Weibo](https://img.shields.io/badge/Weibo-cuzval-yellowgreen.svg)](https://weibo.com/cuzval/)
[![Twitter](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/cuzval)

Expand Down
2 changes: 1 addition & 1 deletion RRNavigationBar.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "RRNavigationBar"
s.version = "0.4.0"
s.version = "0.4.1"
s.summary = "bring `UINavigationBar` to` UIViewController`"

s.homepage = "https://github.com/cuzv/RRNavigationBar.git"
Expand Down
16 changes: 11 additions & 5 deletions RRNavigationBar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
46F46E0B1E7AEEB9000C8011 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 46F46E001E7AEEB9000C8011 /* LaunchScreen.storyboard */; };
46F46E0D1E7AEEB9000C8011 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 46F46E051E7AEEB9000C8011 /* AppDelegate.m */; };
46F46E0F1E7AEEB9000C8011 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 46F46E071E7AEEB9000C8011 /* main.m */; };
B45BD4411FFF46DA009280A5 /* _RRNavigationControllerDelegateImpl.m in Sources */ = {isa = PBXBuildFile; fileRef = B45BD4401FFF46DA009280A5 /* _RRNavigationControllerDelegateImpl.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -88,7 +89,9 @@
46F46E051E7AEEB9000C8011 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
46F46E061E7AEEB9000C8011 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
46F46E071E7AEEB9000C8011 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
B40F69961FFE2E98007C1237 /* RRNavigationControllerDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RRNavigationControllerDelegate.h; sourceTree = "<group>"; };
B45BD43F1FFF46DA009280A5 /* _RRNavigationControllerDelegateImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = _RRNavigationControllerDelegateImpl.h; sourceTree = "<group>"; };
B45BD4401FFF46DA009280A5 /* _RRNavigationControllerDelegateImpl.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = _RRNavigationControllerDelegateImpl.m; sourceTree = "<group>"; };
B48E82781FFF4AC300A90F38 /* UINavigationController+RRNavigationBar_Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UINavigationController+RRNavigationBar_Internal.h"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -198,21 +201,23 @@
4625A81F1E8918070092247A /* RRNavigationBar.h */,
460410F21E82BF9200FC3E2B /* UIViewController+RRNavigationBar.h */,
460410F31E82BF9200FC3E2B /* UIViewController+RRNavigationBar.m */,
B48E82781FFF4AC300A90F38 /* UINavigationController+RRNavigationBar_Internal.h */,
465E1C291E8662830082389A /* UINavigationController+RRNavigationBar.h */,
465E1C2A1E8662830082389A /* UINavigationController+RRNavigationBar.m */,
4625A81E1E8917C20092247A /* UINavigationBar+RRNavigationBar_Internal.h */,
465FE9D41E87E92A000654C1 /* UINavigationBar+RRNavigationBar.h */,
465FE9D51E87E92A000654C1 /* UINavigationBar+RRNavigationBar.m */,
46C3CF1F1F6E884100FF682B /* UIView+RRNavigationBar_internal.h */,
46C3CF201F6E925E00FF682B /* UIView+RRNavigationBar.h */,
46C3CF1D1F6E870200FF682B /* UIView+RRNavigationBar.m */,
4625A8191E88F4B40092247A /* _RRNavigationBar.h */,
4625A81A1E88F4B40092247A /* _RRNavigationBar.m */,
46C646F61E8A377500A3349A /* _RRWeakAssociatedObjectWrapper.h */,
46C646F71E8A377500A3349A /* _RRWeakAssociatedObjectWrapper.m */,
B45BD43F1FFF46DA009280A5 /* _RRNavigationControllerDelegateImpl.h */,
B45BD4401FFF46DA009280A5 /* _RRNavigationControllerDelegateImpl.m */,
465E1C2C1E8662CF0082389A /* RRUtils.h */,
465E1C2D1E8662CF0082389A /* RRUtils.m */,
46C3CF1F1F6E884100FF682B /* UIView+RRNavigationBar_internal.h */,
46C3CF201F6E925E00FF682B /* UIView+RRNavigationBar.h */,
46C3CF1D1F6E870200FF682B /* UIView+RRNavigationBar.m */,
B40F69961FFE2E98007C1237 /* RRNavigationControllerDelegate.h */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -303,6 +308,7 @@
465E1C2B1E8662830082389A /* UINavigationController+RRNavigationBar.m in Sources */,
4625A8311E8938A60092247A /* Helper.m in Sources */,
46C3CF1E1F6E870200FF682B /* UIView+RRNavigationBar.m in Sources */,
B45BD4411FFF46DA009280A5 /* _RRNavigationControllerDelegateImpl.m in Sources */,
466E980F1F7543F900B9BD91 /* NoneTranslucentNextViewController.m in Sources */,
4625A81B1E88F4B40092247A /* _RRNavigationBar.m in Sources */,
466E980C1F7542CA00B9BD91 /* NoneTranslucentViewController.m in Sources */,
Expand Down
3 changes: 1 addition & 2 deletions Sources/RRNavigationBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
#define RRNavigationBar_h

#import "UIViewController+RRNavigationBar.h"
#import "UINavigationController+RRNavigationBar.h"
#import "UINavigationBar+RRNavigationBar.h"
#import "UINavigationController+RRNavigationBar.h"
#import "RRUtils.h"
#import "RRNavigationControllerDelegate.h"

#endif /* RRNavigationBar_h */
39 changes: 0 additions & 39 deletions Sources/RRNavigationControllerDelegate.h

This file was deleted.

5 changes: 2 additions & 3 deletions Sources/UINavigationController+RRNavigationBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

#import <UIKit/UIKit.h>

@protocol RRNavigationControllerDelegate;

@interface UINavigationController (RRNavigationBar)

@property(nullable, nonatomic, weak) id<RRNavigationControllerDelegate> rr_delegate;
/// Acquire the `UINavigationControllerDelegate` you set before.
@property(nullable, nonatomic, weak, readonly) id<UINavigationControllerDelegate> rr_delegate;

@end
108 changes: 28 additions & 80 deletions Sources/UINavigationController+RRNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
// Copyright © 2017 RedRain. All rights reserved.
//

#import "UINavigationController+RRNavigationBar.h"
#import "UINavigationController+RRNavigationBar_Internal.h"
#import "UINavigationBar+RRNavigationBar.h"
#import "UINavigationBar+RRNavigationBar_Internal.h"
#import <objc/runtime.h>
#import "UIViewController+RRNavigationBar.h"
#import "RRUtils.h"
#import "_RRWeakAssociatedObjectWrapper.h"
#import "UIView+RRNavigationBar_internal.h"
#import "RRNavigationControllerDelegate.h"
#import "_RRNavigationControllerDelegateImpl.h"

#ifndef RRRecoverObject
# define RRRecoverObject(from, to, info, property) (to.property = info[@#property] ?: from.property)
Expand All @@ -31,19 +33,6 @@
# define RRRecoverDobule(from, to, info, property) (to.property = info[@#property] ? [info[@#property] doubleValue] : from.property)
#endif

#ifndef RRExcludeImpactBehaviorFor
# define RRExcludeImpactBehaviorFor(instance) \
for (Class clazz in _excludeNVCClassess) { \
if ([instance isKindOfClass:clazz]) { return; } \
} \
for (UINavigationController *nvc in _excludeNVCInstance) { \
if ([instance isEqual:nvc]) { return; } \
}
#endif

static NSMutableSet *_excludeNVCClassess;
static NSMutableSet *_excludeNVCInstance;

void RRNavigationBarExcludeImpactBehaviorForClass(Class _Nonnull nvcClass) {
if (![nvcClass isSubclassOfClass:UINavigationController.class]) {
return;
Expand All @@ -60,9 +49,10 @@ void RRNavigationBarExcludeImpactBehaviorForInstance(__kindof UINavigationContro
[_excludeNVCInstance addObject:nvc];
}

@interface UINavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@interface UINavigationController ()<UIGestureRecognizerDelegate>
@property (nonatomic, weak, nullable) UIViewController *_visibleTopViewController;
@property (nonatomic, assign) BOOL _navigationBarInitialized;
@property (nonatomic, retain, nullable) _RRNavigationControllerDelegateImpl *_delegateImpl;
@end

@implementation UINavigationController (RRNavigationBar)
Expand All @@ -73,6 +63,7 @@ + (void)load {
RRSwizzleInstanceMethod(self.class, @selector(viewDidLoad), @selector(_rr_nvc_viewDidLoad));
RRSwizzleInstanceMethod(self.class, @selector(viewWillLayoutSubviews), @selector(_rr_nvc_viewWillLayoutSubviews));
RRSwizzleInstanceMethod(self.class, @selector(preferredStatusBarStyle), @selector(_rr_nvc_preferredStatusBarStyle));
RRSwizzleInstanceMethod(self.class, @selector(setDelegate:), @selector(_rr_setDelegate:));
});
}

Expand All @@ -88,8 +79,8 @@ - (void)_rr_nvc_viewDidLoad {
});

RRExcludeImpactBehaviorFor(self);

self.delegate = self;
self._delegateImpl = [_RRNavigationControllerDelegateImpl new];
self.delegate = self._delegateImpl;
self.interactivePopGestureRecognizer.delegate = self;
}

Expand Down Expand Up @@ -137,6 +128,14 @@ - (UIStatusBarStyle)_rr_nvc_preferredStatusBarStyle {
return self._rr_nvc_preferredStatusBarStyle;
}

- (void)_rr_setDelegate:(nonnull id<UINavigationControllerDelegate>)delegate {
[self _rr_setDelegate:self._delegateImpl];

if (delegate != self._delegateImpl) {
self._delegateImpl.delegate = delegate;
}
}

#pragma mark - Private

- (nullable UIViewController *)_visibleTopViewController {
Expand All @@ -156,6 +155,18 @@ - (void)set_navigationBarInitialized:(BOOL)_navigationBarInitialized {
objc_setAssociatedObject(self, @selector(_navigationBarInitialized), @(_navigationBarInitialized), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (_RRNavigationControllerDelegateImpl *)_delegateImpl {
return objc_getAssociatedObject(self, _cmd);
}

- (void)set_delegateImpl:(_RRNavigationControllerDelegateImpl *)_delegateImpl {
objc_setAssociatedObject(self, @selector(_delegateImpl), _delegateImpl, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id<UINavigationControllerDelegate>)rr_delegate {
return self._delegateImpl.delegate;
}

#pragma mark -

- (void)_handleWillShowViewController:(UIViewController *)viewController {
Expand Down Expand Up @@ -309,69 +320,6 @@ - (void)_rr_restoreContentInsetAdjustmentBehaviorIfNeededForViewController:(UIVi

#endif

#pragma mark - UINavigationControllerDelegate

- (nullable id <RRNavigationControllerDelegate>)rr_delegate {
return ((_RRWeakAssociatedObjectWrapper *)objc_getAssociatedObject(self, _cmd)).object;
}

- (void)setRr_delegate:(id <RRNavigationControllerDelegate>)rr_delegate {
_RRWeakAssociatedObjectWrapper *wrapper = [[_RRWeakAssociatedObjectWrapper alloc] initWithObject:rr_delegate];
objc_setAssociatedObject(self, @selector(rr_delegate), wrapper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark -

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (self.rr_delegate && [self.rr_delegate respondsToSelector:@selector(rr_navigationController:willShowViewController:animated:)]) {
[self.rr_delegate rr_navigationController:navigationController willShowViewController:viewController animated:animated];
}

RRExcludeImpactBehaviorFor(navigationController);
[self _handleWillShowViewController:viewController];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (self.rr_delegate && [self.rr_delegate respondsToSelector:@selector(rr_navigationController:didShowViewController:animated:)]) {
[self.rr_delegate rr_navigationController:navigationController didShowViewController:viewController animated:animated];
}

RRExcludeImpactBehaviorFor(navigationController);
[self _handleDidShowViewController:viewController];
}

- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController {
if (self.rr_delegate && [self.rr_delegate respondsToSelector:@selector(rr_navigationControllerSupportedInterfaceOrientations:)]) {
return [self.rr_delegate rr_navigationControllerSupportedInterfaceOrientations:navigationController];
}
return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController {
if (self.rr_delegate && [self.rr_delegate respondsToSelector:@selector(rr_navigationControllerPreferredInterfaceOrientationForPresentation:)]) {
return [self.rr_delegate rr_navigationControllerPreferredInterfaceOrientationForPresentation:navigationController];
}
return UIInterfaceOrientationUnknown;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController {
if (self.rr_delegate && [self.rr_delegate respondsToSelector:@selector(rr_navigationController:interactionControllerForAnimationController:)]) {
return [self.rr_delegate rr_navigationController:navigationController interactionControllerForAnimationController:animationController];
}
return nil;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC {
if (self.rr_delegate && [self.rr_delegate respondsToSelector:@selector(rr_navigationController:animationControllerForOperation:fromViewController:toViewController:)]) {
return [self.rr_delegate rr_navigationController:navigationController animationControllerForOperation:operation fromViewController:fromVC toViewController:toVC];
}
return nil;
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
Expand Down
31 changes: 31 additions & 0 deletions Sources/UINavigationController+RRNavigationBar_Internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// UINavigationController+RRNavigationBar_Internal.h
// RRNavigationBar
//
// Created by Roy Shaw on 1/5/18.
// Copyright © 2018 RedRain. All rights reserved.
//

NS_ASSUME_NONNULL_BEGIN

static NSMutableSet *_excludeNVCClassess;
static NSMutableSet *_excludeNVCInstance;

#ifndef RRExcludeImpactBehaviorFor
# define RRExcludeImpactBehaviorFor(instance) \
for (Class clazz in _excludeNVCClassess) { \
if ([instance isKindOfClass:clazz]) { return; } \
} \
for (UINavigationController *nvc in _excludeNVCInstance) { \
if ([instance isEqual:nvc]) { return; } \
}
#endif

@interface UINavigationController (RRNavigationBar_Internal)

- (void)_handleWillShowViewController:(UIViewController *)viewController;
- (void)_handleDidShowViewController:(UIViewController *)viewController;

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions Sources/_RRNavigationControllerDelegateImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// _RRNavigationControllerDelegateImpl.h
// RRNavigationBar
//
// Created by Roy Shaw on 1/5/18.
// Copyright © 2018 RedRain. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface _RRNavigationControllerDelegateImpl : NSObject <UINavigationControllerDelegate>

@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;

@end
Loading

0 comments on commit 0685e9a

Please sign in to comment.