Skip to content

Commit

Permalink
Fix topBar.background.color on iOS 13 (#5685)
Browse files Browse the repository at this point in the history
  • Loading branch information
yogevbd authored and guyca committed Nov 26, 2019
1 parent b53bbf3 commit 6f13d69
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 31 deletions.
33 changes: 2 additions & 31 deletions lib/ios/RNNStackController.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import "RNNStackController.h"
#import "RNNComponentViewController.h"

const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;
#import "UINavigationBar+utils.h"

@implementation RNNStackController

Expand Down Expand Up @@ -52,35 +51,7 @@ - (UIViewController *)childViewControllerForStatusBarStyle {
}

- (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {
if (backgroundColor) {
CGFloat bgColorAlpha = CGColorGetAlpha(backgroundColor.CGColor);

if (bgColorAlpha == 0.0) {
if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
transparentView.backgroundColor = [UIColor clearColor];
transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
[self.navigationBar insertSubview:transparentView atIndex:0];
}
self.navigationBar.translucent = YES;
[self.navigationBar setBackgroundColor:[UIColor clearColor]];
self.navigationBar.shadowImage = [UIImage new];
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
} else {
self.navigationBar.barTintColor = backgroundColor;
UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
if (transparentView){
[transparentView removeFromSuperview];
}
}
} else {
UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
if (transparentView){
[transparentView removeFromSuperview];
}

self.navigationBar.barTintColor = nil;
}
[self.navigationBar rnn_setBackgroundColor:backgroundColor];
}

@end
8 changes: 8 additions & 0 deletions lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
509B258F2178BE7A00C83C23 /* RNNStackPresenterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 509B258E2178BE7A00C83C23 /* RNNStackPresenterTest.m */; };
50A00C37200F84D6000F01A6 /* RNNOverlayOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 50A00C35200F84D6000F01A6 /* RNNOverlayOptions.h */; };
50A00C38200F84D6000F01A6 /* RNNOverlayOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A00C36200F84D6000F01A6 /* RNNOverlayOptions.m */; };
50A409CA238D444900D5FF7D /* UINavigationBar+utils.h in Headers */ = {isa = PBXBuildFile; fileRef = 50A409C8238D444900D5FF7D /* UINavigationBar+utils.h */; };
50A409CB238D444900D5FF7D /* UINavigationBar+utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A409C9238D444900D5FF7D /* UINavigationBar+utils.m */; };
50AB0B1C2255F8640039DAED /* UIViewController+LayoutProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 50AB0B1A2255F8640039DAED /* UIViewController+LayoutProtocol.h */; };
50AB0B1D2255F8640039DAED /* UIViewController+LayoutProtocol.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AB0B1B2255F8640039DAED /* UIViewController+LayoutProtocol.m */; };
50AB0B1F22562FA10039DAED /* UIViewController+LayoutProtocolTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AB0B1E22562FA10039DAED /* UIViewController+LayoutProtocolTest.m */; };
Expand Down Expand Up @@ -558,6 +560,8 @@
509B258E2178BE7A00C83C23 /* RNNStackPresenterTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNStackPresenterTest.m; sourceTree = "<group>"; };
50A00C35200F84D6000F01A6 /* RNNOverlayOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNOverlayOptions.h; sourceTree = "<group>"; };
50A00C36200F84D6000F01A6 /* RNNOverlayOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNOverlayOptions.m; sourceTree = "<group>"; };
50A409C8238D444900D5FF7D /* UINavigationBar+utils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UINavigationBar+utils.h"; sourceTree = "<group>"; };
50A409C9238D444900D5FF7D /* UINavigationBar+utils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UINavigationBar+utils.m"; sourceTree = "<group>"; };
50AB0B1A2255F8640039DAED /* UIViewController+LayoutProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIViewController+LayoutProtocol.h"; sourceTree = "<group>"; };
50AB0B1B2255F8640039DAED /* UIViewController+LayoutProtocol.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+LayoutProtocol.m"; sourceTree = "<group>"; };
50AB0B1E22562FA10039DAED /* UIViewController+LayoutProtocolTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+LayoutProtocolTest.m"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1173,6 +1177,8 @@
E5F6C39F22DB4D0E0093C2CE /* UIView+Utils.m */,
E5F6C39D22DB4D0E0093C2CE /* UIViewController+Utils.h */,
E5F6C39E22DB4D0E0093C2CE /* UIViewController+Utils.m */,
50A409C8238D444900D5FF7D /* UINavigationBar+utils.h */,
50A409C9238D444900D5FF7D /* UINavigationBar+utils.m */,
);
name = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -1326,6 +1332,7 @@
E8E518321F83B3E0000467AC /* RNNUtils.h in Headers */,
50E5F791223FA04C002AFEAD /* RNNAnimationConfigurationOptions.h in Headers */,
5049594E216F6277006D2B81 /* NumberParser.h in Headers */,
50A409CA238D444900D5FF7D /* UINavigationBar+utils.h in Headers */,
50BE951320B5A787004F5DF5 /* RNNStatusBarOptions.h in Headers */,
50570BEA2063E09B006A1B5C /* RNNTitleViewHelper.h in Headers */,
50495956216F6B3D006D2B81 /* DictionaryParser.h in Headers */,
Expand Down Expand Up @@ -1512,6 +1519,7 @@
buildActionMask = 2147483647;
files = (
50C4A497206BDDBB00DB292E /* RNNSubtitleOptions.m in Sources */,
50A409CB238D444900D5FF7D /* UINavigationBar+utils.m in Sources */,
50AB0B1D2255F8640039DAED /* UIViewController+LayoutProtocol.m in Sources */,
263905B41E4C6F440023D7D3 /* MMDrawerVisualState.m in Sources */,
5012240B21735959000F5F98 /* RNNSideMenuPresenter.m in Sources */,
Expand Down
9 changes: 9 additions & 0 deletions lib/ios/UINavigationBar+utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <UIKit/UIKit.h>

@interface UINavigationBar (utils)

- (void)rnn_setBackgroundColor:(UIColor *)color;

- (void)rnn_setBackgroundColorTransparent;

@end
82 changes: 82 additions & 0 deletions lib/ios/UINavigationBar+utils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#import "UINavigationBar+utils.h"

const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;

@implementation UINavigationBar (utils)

- (void)rnn_setBackgroundColor:(UIColor *)color {
CGFloat bgColorAlpha = CGColorGetAlpha(color.CGColor);

if (color && bgColorAlpha == 0.0) {
[self rnn_setBackgroundColorTransparent];
} else {
[self removeTransparentView];
if (@available(iOS 13.0, *)) {
[self getNavigaitonBarStandardAppearance].backgroundColor = color;
[self getNavigaitonBarCompactAppearance].backgroundColor = color;
[self getNavigaitonBarScrollEdgeAppearance].backgroundColor = color;
}
self.barTintColor = color;
}
}

- (void)rnn_setBackgroundColorTransparent {
if (![self viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
transparentView.backgroundColor = [UIColor clearColor];
transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
[self insertSubview:transparentView atIndex:0];
}

self.translucent = YES;
[self setBackgroundColor:[UIColor clearColor]];
self.shadowImage = [UIImage new];
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
if (@available(iOS 13.0, *)) {
UINavigationBarAppearance *standardAppearance = [self getNavigaitonBarStandardAppearance];
standardAppearance.backgroundColor = [UIColor clearColor];
standardAppearance.shadowImage = [UIImage new];
standardAppearance.backgroundImage = [UIImage new];

UINavigationBarAppearance *compactAppearance = [self getNavigaitonBarCompactAppearance];
compactAppearance.backgroundColor = [UIColor clearColor];
compactAppearance.shadowImage = [UIImage new];
compactAppearance.backgroundImage = [UIImage new];

UINavigationBarAppearance *scrollEdgeAppearance = [self getNavigaitonBarScrollEdgeAppearance];
scrollEdgeAppearance.backgroundColor = [UIColor clearColor];
scrollEdgeAppearance.shadowImage = [UIImage new];
scrollEdgeAppearance.backgroundImage = [UIImage new];
}
}

- (void)removeTransparentView {
UIView *transparentView = [self viewWithTag:TOP_BAR_TRANSPARENT_TAG];
if (transparentView){
[transparentView removeFromSuperview];
}
}


- (UINavigationBarAppearance*)getNavigaitonBarStandardAppearance API_AVAILABLE(ios(13.0)) {
if (!self.standardAppearance) {
self.standardAppearance = [UINavigationBarAppearance new];
}
return self.standardAppearance;
}

- (UINavigationBarAppearance*)getNavigaitonBarCompactAppearance API_AVAILABLE(ios(13.0)) {
if (!self.compactAppearance) {
self.compactAppearance = [UINavigationBarAppearance new];
}
return self.compactAppearance;
}

- (UINavigationBarAppearance*)getNavigaitonBarScrollEdgeAppearance API_AVAILABLE(ios(13.0)) {
if (!self.scrollEdgeAppearance) {
self.scrollEdgeAppearance = [UINavigationBarAppearance new];
}
return self.scrollEdgeAppearance;
}

@end
9 changes: 9 additions & 0 deletions playground/ios/NavigationTests/RNNNavigationControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ - (void)testSetTopBarBackgroundColor_ShouldSetBackgroundColor {
UIColor* color = UIColor.redColor;
[self.uut setTopBarBackgroundColor:color];
XCTAssertEqual(self.uut.navigationBar.barTintColor, color);
XCTAssertEqual(self.uut.navigationBar.standardAppearance.backgroundColor, color);
XCTAssertEqual(self.uut.navigationBar.compactAppearance.backgroundColor, color);
XCTAssertEqual(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor, color);
}

- (void)testSetTopBarBackgroundColor_ShouldSetTransparentBackgroundColor {
Expand All @@ -166,6 +169,9 @@ - (void)testSetTopBarBackgroundColor_ShouldSetTransparentBackgroundColor {
XCTAssertTrue(self.uut.navigationBar.translucent);
XCTAssertNotNil(self.uut.navigationBar.shadowImage);
XCTAssertNotNil([self.uut.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]);
XCTAssertNil(self.uut.navigationBar.standardAppearance.backgroundColor);
XCTAssertNil(self.uut.navigationBar.compactAppearance.backgroundColor);
XCTAssertNil(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor);
}

- (void)testSetTopBarBackgroundColor_ShouldRemoveTransparentView {
Expand All @@ -188,6 +194,9 @@ - (void)testSetTopBarBackgroundColor_NilColorShouldResetNavigationBar {

XCTAssertNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
XCTAssertNil(self.uut.navigationBar.barTintColor);
XCTAssertNil(self.uut.navigationBar.standardAppearance.backgroundColor);
XCTAssertNil(self.uut.navigationBar.compactAppearance.backgroundColor);
XCTAssertNil(self.uut.navigationBar.scrollEdgeAppearance.backgroundColor);
}

- (RNNStackController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {
Expand Down

0 comments on commit 6f13d69

Please sign in to comment.