Skip to content

Commit

Permalink
Fix transparent topBar background color transition (#5317)
Browse files Browse the repository at this point in the history
  • Loading branch information
yogevbd authored Jul 25, 2019
1 parent bcf2f28 commit 147cf4c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
29 changes: 1 addition & 28 deletions lib/ios/RNNNavigationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

const NSInteger TOP_BAR_TRANSPARENT_TAG = 78264803;

@interface RNNNavigationController()

@property (nonatomic, strong) NSMutableDictionary* originalTopBarImages;

@end

@implementation RNNNavigationController

- (void)viewDidLayoutSubviews {
Expand All @@ -22,7 +16,7 @@ - (UIViewController *)getCurrentChild {
}

- (CGFloat)getTopBarHeight {
return self.navigationBar.frame.size.height;
return self.navigationBar.frame.size.height;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
Expand Down Expand Up @@ -63,7 +57,6 @@ - (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {

if (bgColorAlpha == 0.0) {
if (![self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]){
[self storeOriginalTopBarImages:self];
UIView *transparentView = [[UIView alloc] initWithFrame:CGRectZero];
transparentView.backgroundColor = [UIColor clearColor];
transparentView.tag = TOP_BAR_TRANSPARENT_TAG;
Expand All @@ -78,36 +71,16 @@ - (void)setTopBarBackgroundColor:(UIColor *)backgroundColor {
UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
if (transparentView){
[transparentView removeFromSuperview];
[self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"];
self.originalTopBarImages = nil;
}
}
} else {
UIView *transparentView = [self.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG];
if (transparentView){
[transparentView removeFromSuperview];
[self.navigationBar setBackgroundImage:self.originalTopBarImages[@"backgroundImage"] ? self.originalTopBarImages[@"backgroundImage"] : [self.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = self.originalTopBarImages[@"shadowImage"] ? self.originalTopBarImages[@"shadowImage"] : self.navigationBar.shadowImage;
self.originalTopBarImages = nil;
}

self.navigationBar.barTintColor = nil;
}
}

- (void)storeOriginalTopBarImages:(UINavigationController *)navigationController {
NSMutableDictionary *originalTopBarImages = [@{} mutableCopy];
UIImage *bgImage = [navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
if (bgImage != nil) {
originalTopBarImages[@"backgroundImage"] = bgImage;
}
UIImage *shadowImage = navigationController.navigationBar.shadowImage;
if (shadowImage != nil) {
originalTopBarImages[@"shadowImage"] = shadowImage;
}
self.originalTopBarImages = originalTopBarImages;
}


@end
38 changes: 38 additions & 0 deletions lib/ios/ReactNativeNavigationTests/RNNNavigationControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,44 @@ - (void)testOverrideOptionsShouldOverrideOptionsState {
[(id)self.uut.options verify];
}

- (void)testSetTopBarBackgroundColor_ShouldSetBackgroundColor {
UIColor* color = UIColor.redColor;
[self.uut setTopBarBackgroundColor:color];
XCTAssertEqual(self.uut.navigationBar.barTintColor, color);
}

- (void)testSetTopBarBackgroundColor_ShouldSetTransparentBackgroundColor {
UIColor* transparentColor = UIColor.clearColor;
[self.uut setTopBarBackgroundColor:transparentColor];

XCTAssertEqual(self.uut.navigationBar.backgroundColor, transparentColor);
XCTAssertTrue(self.uut.navigationBar.translucent);
XCTAssertNotNil(self.uut.navigationBar.shadowImage);
XCTAssertNotNil([self.uut.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]);
}

- (void)testSetTopBarBackgroundColor_ShouldRemoveTransparentView {
UIColor* transparentColor = UIColor.clearColor;
UIColor* redColor = UIColor.redColor;

[self.uut setTopBarBackgroundColor:transparentColor];
XCTAssertNotNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
[self.uut setTopBarBackgroundColor:redColor];
XCTAssertNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
}

- (void)testSetTopBarBackgroundColor_NilColorShouldResetNavigationBar {
UIColor* transparentColor = UIColor.clearColor;
UIColor* redColor = UIColor.redColor;

[self.uut setTopBarBackgroundColor:transparentColor];
[self.uut setTopBarBackgroundColor:redColor];
[self.uut setTopBarBackgroundColor:nil];

XCTAssertNil([self.uut.navigationBar viewWithTag:TOP_BAR_TRANSPARENT_TAG]);
XCTAssertNil(self.uut.navigationBar.barTintColor);
}

- (RNNNavigationController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {
RNNNavigationController* nav = [[RNNNavigationController alloc] initWithLayoutInfo:nil creator:_creator options:options defaultOptions:nil presenter:[[RNNNavigationControllerPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1]];
return nav;
Expand Down

0 comments on commit 147cf4c

Please sign in to comment.