Skip to content

Commit

Permalink
Workaround for present nvc, fixed global appearance not working when …
Browse files Browse the repository at this point in the history
…setup in viewDidLoad;
  • Loading branch information
cuzv committed Mar 29, 2017
1 parent 6c78169 commit c183e10
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 22 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@


- Update rr_forceShadowImageHidden impl;
- Update demo;
- Update demo;

### 0.2.1

- Workaround for present nvc, fixed global appearance not working when setup in viewDidLoad;
16 changes: 6 additions & 10 deletions Example/EmbedTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ @implementation EmbedTabBarController
- (void)viewDidLoad {
[super viewDidLoad];

// {
// UINavigationBar *global = [UINavigationBar appearance];
// global.tintColor = [UIColor magentaColor];
// global.barStyle = UIBarMetricsDefault;
// global.translucent = YES;
// [global setBackgroundImage:RRUIImageMake([UIColor cyanColor]) forBarMetrics:UIBarMetricsDefault];
// UIImage *backIndicator = [RRUIImageMakeWithSize([UIColor yellowColor], CGSizeMake(13, 21)) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// global.backIndicatorImage = backIndicator;
// global.backIndicatorTransitionMaskImage = backIndicator;
// }
{
UINavigationBar *global = [UINavigationBar appearance];
global.tintColor = [UIColor magentaColor];
global.barStyle = UIBarMetricsDefault;
global.translucent = YES;
}

UINavigationController *productRoot = [[UINavigationController alloc] initWithRootViewController:[ProductListViewController new]];
productRoot.tabBarItem.title = @"Product Example";
Expand Down
2 changes: 1 addition & 1 deletion Example/Product/ProductListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"Product List";

// NSLog(@"%@", self.rr_navigationBar);
NSLog(@"ProductListViewController viewDidLoad rr_navigationBar: %@", self.rr_navigationBar);

// [self.rr_navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// self.rr_navigationBar.shadowImage = [UIImage new];
Expand Down
2 changes: 1 addition & 1 deletion Example/Storyboard/Present.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<view key="view" contentMode="scaleToFill" id="Q84-OF-rhm">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
</view>
<navigationItem key="navigationItem" title="storyboard" id="rpi-IB-hqq">
<barButtonItem key="leftBarButtonItem" title="dismiss" id="CgX-B1-7k2">
Expand Down
6 changes: 6 additions & 0 deletions Example/Storyboard/SBRootViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ @implementation SBRootViewController
- (void)viewDidLoad {
[super viewDidLoad];

NSLog(@"SBRootViewController viewDidLoad rr_navigationBar: %@", self.rr_navigationBar);
[self.rr_navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.rr_navigationBar.shadowImage = [UIImage new];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"SBRootViewController viewWillAppear rr_navigationBar: %@", self.rr_navigationBar);
}

- (IBAction)handleClickDimsiss:(UIBarButtonItem *)sender {
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
Expand Down
1 change: 1 addition & 0 deletions Example/Storyboard/StoryboardViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "StoryboardViewController.h"
#import "SBRootViewController.h"

@interface StoryboardViewController ()

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.2.0"
s.version = "0.2.1"
s.summary = "bring `UINavigationBar` to` UIViewController`"

s.homepage = "https://github.com/cuzv/RRNavigationBar.git"
Expand Down
11 changes: 11 additions & 0 deletions Sources/UINavigationBar+RRAddition.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ - (void)setRr_forceShadowImageHidden:(BOOL)rr_forceShadowImageHidden {
self._holder.view.window) {
self._holder.navigationController.navigationBar.rr_forceShadowImageHidden = rr_forceShadowImageHidden;
}
if (self._tmpInfo) {
self._tmpInfo[@"rr_forceShadowImageHidden"] = @(rr_forceShadowImageHidden);
}
}

- (nullable NSMutableDictionary<NSString *, id> *)_tmpInfo {
return objc_getAssociatedObject(self, _cmd);
}

- (void)set_tmpInfo:(nullable NSMutableDictionary<NSString *, id> *)_tmpInfo {
objc_setAssociatedObject(self, @selector(_tmpInfo), _tmpInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
1 change: 1 addition & 0 deletions Sources/UINavigationBar+RRAddition_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
@property (nonatomic, assign) BOOL _rr_transiting;
@property (nonatomic, weak, nullable) UIViewController *_holder;
- (void)_apply;
@property (nonatomic, retain, nullable) NSMutableDictionary<NSString *, id> *_tmpInfo;

@end
71 changes: 70 additions & 1 deletion Sources/UINavigationController+RRNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <objc/runtime.h>
#import "UINavigationBar+RRAddition_Internal.h"
#import "_RRWeakAssociatedObjectWrapper.h"
#import "UINavigationBar+RRAddition.h"

@interface UINavigationController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, weak, nullable) UIViewController *_visibleTopViewController;
Expand Down Expand Up @@ -41,10 +42,78 @@ - (void)_rr_nvc_viewDidLoad {

- (void)_rr_nvc_viewWillLayoutSubviews {
[self _rr_nvc_viewWillLayoutSubviews];
if (!self._navigationBarInitialized) {
if (!self._navigationBarInitialized && self.navigationBar) {
self.rr_navigationBar = RRUINavigationBarDuplicate(self.navigationBar);
[self.rr_navigationBar _apply];
self._navigationBarInitialized = YES;
RRLog(@"nvc's rr_navigationBar initialized");
}

// Workaround for present nvc.
// Normally call order nvc:viewWillLayoutSubviews -> vc:viewDidLoad,
// when present a nvc, vc:viewDidLoad -> nvc:viewWillLayoutSubviews.
// So, in vc:viewDidLoad vc.rr_navigationBar isn't the correct styles,
// therefore recored setups, recover when nvc.rr_navigationBar initialized.
NSDictionary *info = self.topViewController.rr_navigationBar._tmpInfo;
if (info) {
UINavigationBar *this = self.rr_navigationBar;
UINavigationBar *bar = self.topViewController.rr_navigationBar;
if (info[@"barStyle"]) {
bar.barStyle = [info[@"barStyle"] integerValue];
} else {
bar.barStyle = this.barStyle;
}
if (info[@"translucent"]) {
bar.barStyle = [info[@"translucent"] boolValue];
} else {
bar.translucent = this.translucent;
}
if (info[@"tintColor"]) {
bar.tintColor = info[@"tintColor"];
} else {
bar.tintColor = this.tintColor;
}
if (info[@"barTintColor"]) {
bar.barTintColor = info[@"barTintColor"];
} else {
bar.barTintColor = this.barTintColor;
}
if (info[@"backgroundColor"]) {
bar.backgroundColor = info[@"backgroundColor"];
} else {
bar.backgroundColor = this.backgroundColor;
}
if (info[@"shadowImage"]) {
bar.shadowImage = info[@"shadowImage"];
} else {
bar.shadowImage = this.shadowImage;
}
if (info[@"backgroundImage"]) {
[bar setBackgroundImage:info[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
} else {
[bar setBackgroundImage:[this backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
}
if (info[@"alpha"]) {
bar.alpha = [info[@"alpha"] doubleValue];
} else {
bar.alpha = this.alpha;
}
if (info[@"backIndicatorImage"]) {
bar.backIndicatorImage = info[@"backIndicatorImage"];
} else {
bar.backIndicatorImage = this.backIndicatorImage;
}
if (info[@"backIndicatorTransitionMaskImage"]) {
bar.backIndicatorTransitionMaskImage = info[@"backIndicatorTransitionMaskImage"];
} else {
bar.backIndicatorTransitionMaskImage = this.backIndicatorTransitionMaskImage;
}
if (info[@"rr_forceShadowImageHidden"]) {
bar.rr_forceShadowImageHidden = [info[@"rr_forceShadowImageHidden"] boolValue];
} else {
bar.rr_forceShadowImageHidden = this.rr_forceShadowImageHidden;
}
self.topViewController.rr_navigationBar._tmpInfo = nil;
}
}

Expand Down
19 changes: 12 additions & 7 deletions Sources/UIViewController+RRNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@ - (nonnull UINavigationBar *)rr_navigationBar {
UINavigationController *nvc = self.navigationController;
if (nvc) {
UINavigationBar *navigationBar = nvc.rr_navigationBar;
BOOL flag = NO;
if (!navigationBar) {
// prevent return nil when present nvc.
// See UINavigationController+RRNavigationBar.m `_rr_nvc_viewWillLayoutSubviews` for less part logic.
navigationBar = nvc.navigationBar;

// When load navigationController's rootViewController from nib,
// rootViewController's viewDidLoad called before navigationController's viewWillLayoutSubviews method.
nvc.rr_navigationBar = RRUINavigationBarDuplicate(navigationBar);
[nvc setValue:@(YES) forKey:@"_navigationBarInitialized"];
// mark as temp, will replace after nvc's rr_navigationBar initialized.
flag = YES;
}
if (navigationBar) {
bar = RRUINavigationBarDuplicate(navigationBar);
self.rr_navigationBar = bar;
}
if (flag) {
self.rr_navigationBar._tmpInfo = [@{} mutableCopy];
}
bar = RRUINavigationBarDuplicate(navigationBar);
self.rr_navigationBar = bar;
}
return bar;
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/_RRNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ - (void)setBarStyle:(UIBarStyle)barStyle {
self._holder.navigationController.navigationBar.barStyle = barStyle;
}
[super setBarStyle:barStyle];
if (self._tmpInfo) {
self._tmpInfo[@"barStyle"] = @(barStyle);
}
}

- (void)setTranslucent:(BOOL)translucent {
Expand All @@ -30,6 +33,9 @@ - (void)setTranslucent:(BOOL)translucent {
self._holder.navigationController.navigationBar.translucent = translucent;
}
[super setTranslucent:translucent];
if (self._tmpInfo) {
self._tmpInfo[@"translucent"] = @(translucent);
}
}

- (void)setTintColor:(UIColor *)tintColor {
Expand All @@ -39,6 +45,9 @@ - (void)setTintColor:(UIColor *)tintColor {
self._holder.navigationController.navigationBar.tintColor = tintColor;
}
[super setTintColor:tintColor];
if (self._tmpInfo) {
self._tmpInfo[@"tintColor"] = tintColor;
}
}

- (void)setBarTintColor:(UIColor *)barTintColor {
Expand All @@ -48,6 +57,9 @@ - (void)setBarTintColor:(UIColor *)barTintColor {
self._holder.navigationController.navigationBar.barTintColor = barTintColor;
}
[super setBarTintColor:barTintColor];
if (self._tmpInfo) {
self._tmpInfo[@"barTintColor"] = barTintColor;
}
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
Expand All @@ -57,6 +69,9 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor {
self._holder.navigationController.navigationBar.backgroundColor = backgroundColor;
}
[super setBackgroundColor:backgroundColor];
if (self._tmpInfo) {
self._tmpInfo[@"backgroundColor"] = backgroundColor;
}
}

- (void)setShadowImage:(UIImage *)shadowImage {
Expand All @@ -66,6 +81,9 @@ - (void)setShadowImage:(UIImage *)shadowImage {
self._holder.navigationController.navigationBar.shadowImage = shadowImage;
}
[super setShadowImage:shadowImage];
if (self._tmpInfo) {
self._tmpInfo[@"shadowImage"] = shadowImage;
}
}

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics {
Expand All @@ -75,6 +93,9 @@ - (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetric
[self._holder.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:barMetrics];
}
[super setBackgroundImage:backgroundImage forBarMetrics:barMetrics];
if (self._tmpInfo) {
self._tmpInfo[@"backgroundImage"] = backgroundImage;
}
}

- (void)setAlpha:(CGFloat)alpha {
Expand All @@ -84,6 +105,9 @@ - (void)setAlpha:(CGFloat)alpha {
self._holder.navigationController.navigationBar.alpha = alpha;
}
[super setAlpha:alpha];
if (self._tmpInfo) {
self._tmpInfo[@"alpha"] = @(alpha);
}
}

- (void)setBackIndicatorImage:(UIImage *)backIndicatorImage {
Expand All @@ -93,6 +117,9 @@ - (void)setBackIndicatorImage:(UIImage *)backIndicatorImage {
self._holder.navigationController.navigationBar.backIndicatorImage = backIndicatorImage;
}
[super setBackIndicatorImage:backIndicatorImage];
if (self._tmpInfo) {
self._tmpInfo[@"backIndicatorImage"] = backIndicatorImage;
}
}

- (void)setBackIndicatorTransitionMaskImage:(UIImage *)backIndicatorTransitionMaskImage {
Expand All @@ -102,6 +129,9 @@ - (void)setBackIndicatorTransitionMaskImage:(UIImage *)backIndicatorTransitionMa
self._holder.navigationController.navigationBar.backIndicatorTransitionMaskImage = backIndicatorTransitionMaskImage;
}
[super setBackIndicatorTransitionMaskImage:backIndicatorTransitionMaskImage];
if (self._tmpInfo) {
self._tmpInfo[@"backIndicatorTransitionMaskImage"] = backIndicatorTransitionMaskImage;
}
}

@end

0 comments on commit c183e10

Please sign in to comment.