Skip to content

Commit bceab58

Browse files
committed
Update navigation bar's title and tint color on #resetTo / #replace.
* Approach borrowed from: facebook#1403 (comment) * Would need to follow similar pattern to update other navigation / nav bar props on resetTo / replace, but want to limit the changes from RN master for now.
1 parent 9fd57c5 commit bceab58

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

React/Views/RCTNavItem.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
*/
99

1010
#import <UIKit/UIKit.h>
11-
11+
#import "RCTWrapperViewController.h"
1212
#import "RCTComponent.h"
1313

1414
@interface RCTNavItem : UIView
1515

1616
@property (nonatomic, copy) NSString *title;
17+
@property (nonatomic, weak) RCTWrapperViewController *delegate;
1718
@property (nonatomic, strong) UIImage *leftButtonIcon;
1819
@property (nonatomic, copy) NSString *leftButtonTitle;
1920
@property (nonatomic, strong) UIImage *rightButtonIcon;

React/Views/RCTNavItem.m

+17
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,27 @@
1111

1212
@implementation RCTNavItem
1313

14+
@synthesize delegate = _delegate;
1415
@synthesize backButtonItem = _backButtonItem;
1516
@synthesize leftButtonItem = _leftButtonItem;
1617
@synthesize rightButtonItem = _rightButtonItem;
1718

19+
- (void)setTitle:(NSString *)title
20+
{
21+
if (title != _title) {
22+
_title = title;
23+
[self.delegate titleDidChange:title];
24+
}
25+
}
26+
27+
- (void)setBarTintColor:(UIColor *)barTintColor
28+
{
29+
if (barTintColor != _barTintColor) {
30+
_barTintColor = barTintColor;
31+
[self.delegate barTintColorDidChange:barTintColor];
32+
}
33+
}
34+
1835
- (void)setBackButtonTitle:(NSString *)backButtonTitle
1936
{
2037
_backButtonTitle = backButtonTitle;

React/Views/RCTWrapperViewController.h

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ didMoveToNavigationController:(UINavigationController *)navigationController;
2626
- (instancetype)initWithContentView:(UIView *)contentView NS_DESIGNATED_INITIALIZER;
2727
- (instancetype)initWithNavItem:(RCTNavItem *)navItem;
2828

29+
- (void)titleDidChange:(NSString *)title;
30+
- (void)barTintColorDidChange:(UIColor *)barTintColor;
31+
2932
@property (nonatomic, weak) id<RCTWrapperViewControllerNavigationListener> navigationListener;
3033
@property (nonatomic, strong) RCTNavItem *navItem;
3134

React/Views/RCTWrapperViewController.m

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ - (instancetype)initWithNavItem:(RCTNavItem *)navItem
4545
{
4646
if ((self = [self initWithContentView:navItem])) {
4747
_navItem = navItem;
48+
_navItem.delegate = self;
4849
}
4950
return self;
5051
}
@@ -60,6 +61,19 @@ - (void)viewWillLayoutSubviews
6061
_currentBottomLayoutGuide = self.bottomLayoutGuide;
6162
}
6263

64+
65+
- (void)titleDidChange:(NSString *)title
66+
{
67+
UINavigationItem *item = self.navigationItem;
68+
item.title = title;
69+
}
70+
71+
- (void)barTintColorDidChange:(UIColor *)barTintColor
72+
{
73+
UINavigationBar *bar = self.navigationController.navigationBar;
74+
bar.barTintColor = barTintColor;
75+
}
76+
6377
static BOOL RCTFindScrollViewAndRefreshContentInsetInView(UIView *view)
6478
{
6579
if ([view conformsToProtocol:@protocol(RCTAutoInsetsProtocol)]) {

0 commit comments

Comments
 (0)