Skip to content

Commit d2b9e7f

Browse files
authored
Navigation bar hierarchy fix (#8252)
* Navigation bar hierarchy fix * fix for comments
1 parent 0b04656 commit d2b9e7f

5 files changed

Lines changed: 75 additions & 18 deletions

File tree

ios/UINavigationController+RNNOptions.mm

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,52 @@ - (void)setNavigationBarClipsToBounds:(BOOL)clipsToBounds {
6262
self.navigationBar.clipsToBounds = clipsToBounds;
6363
}
6464

65-
- (void)setBackButtonTestID:(NSString *)testID {
66-
UIView *navigationBarContentView =
65+
- (UIView *)rnn_findBackButtonView {
66+
UIView *contentView =
6767
[self.navigationBar findChildByClass:NSClassFromString(@"_UINavigationBarContentView")];
68-
UIView *barButton =
69-
[navigationBarContentView findChildByClass:NSClassFromString(@"_UIButtonBarButton")];
70-
if (barButton)
71-
barButton.accessibilityIdentifier = testID;
68+
if (!contentView) {
69+
contentView =
70+
[self.navigationBar findChildByClass:NSClassFromString(@"UIKit.NavigationBarContentView")];
71+
}
72+
if (!contentView)
73+
return nil;
74+
75+
Class buttonClass = NSClassFromString(@"_UIButtonBarButton");
76+
77+
return [contentView findDescendantByClass:buttonClass
78+
passingTest:^BOOL(UIView *view) {
79+
if ([view respondsToSelector:NSSelectorFromString(@"isBackButton")]) {
80+
return [[view valueForKey:@"backButton"] boolValue];
81+
}
82+
return YES;
83+
}];
84+
}
85+
86+
- (void)rnn_applyTestID:(NSString *)testID toBackButtonView:(UIView *)barButton {
87+
barButton.accessibilityIdentifier = testID;
88+
barButton.isAccessibilityElement = YES;
89+
}
90+
91+
- (void)setBackButtonTestID:(NSString *)testID {
92+
if (!testID)
93+
return;
94+
95+
UIView *barButton = [self rnn_findBackButtonView];
96+
if (barButton) {
97+
[self rnn_applyTestID:testID toBackButtonView:barButton];
98+
} else {
99+
__weak UINavigationController *weakSelf = self;
100+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)),
101+
dispatch_get_main_queue(), ^{
102+
UINavigationController *nc = weakSelf;
103+
if (!nc)
104+
return;
105+
UIView *btn = [nc rnn_findBackButtonView];
106+
if (btn) {
107+
[nc rnn_applyTestID:testID toBackButtonView:btn];
108+
}
109+
});
110+
}
72111
}
73112

74113
- (CGFloat)getTopBarHeight {

ios/Utils/UIView+Utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ typedef NS_ENUM(NSInteger, ViewType) {
1212

1313
- (UIView *)findChildByClass:clazz;
1414

15+
- (UIView *)findDescendantByClass:clazz;
16+
17+
- (UIView *)findDescendantByClass:(Class)clazz passingTest:(BOOL (^)(UIView *view))test;
18+
1519
- (ViewType)viewType;
1620

1721
- (void)stopMomentumScrollViews;

ios/Utils/UIView+Utils.mm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ - (UIView *)findChildByClass:(id)clazz {
1818
return nil;
1919
}
2020

21+
- (UIView *)findDescendantByClass:(id)clazz {
22+
for (UIView *child in [self subviews]) {
23+
if ([child isKindOfClass:clazz])
24+
return child;
25+
UIView *found = [child findDescendantByClass:clazz];
26+
if (found)
27+
return found;
28+
}
29+
return nil;
30+
}
31+
32+
- (UIView *)findDescendantByClass:(Class)clazz passingTest:(BOOL (^)(UIView *view))test {
33+
for (UIView *child in [self subviews]) {
34+
if ([child isKindOfClass:clazz] && test(child))
35+
return child;
36+
UIView *found = [child findDescendantByClass:clazz passingTest:test];
37+
if (found)
38+
return found;
39+
}
40+
return nil;
41+
}
42+
2143
- (ViewType)viewType {
2244
#ifdef RCT_NEW_ARCH_ENABLED
2345
if ([self isKindOfClass:[RCTImageComponentView class]]) {

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"eslint-plugin-jest": "^28.11.0",
124124
"eslint-plugin-prettier": "3.1.4",
125125
"github-release-notes": "https://github.com/yogevbd/github-release-notes/tarball/e601b3dba72dcd6cba323c1286ea6dd0c0110b58",
126-
"husky": "4.2.5",
126+
127127
"identity-obj-proxy": "3.0.0",
128128
"jest": "^29.6.3",
129129
"lint-staged": "10.2.11",
@@ -152,11 +152,7 @@
152152
"playground"
153153
],
154154
"packageManager": "yarn@4.12.0",
155-
"husky": {
156-
"hooks": {
157-
"pre-commit": "lint-staged"
158-
}
159-
},
155+
160156
"lint-staged": {
161157
"*.{js,ts,tsx}": "eslint --fix",
162158
"*.{h,m,mm}": "node ./scripts/check-clang-format",

playground/package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"eslint-plugin-jest": "^28.11.0",
6262
"eslint-plugin-prettier": "3.1.4",
6363
"github-release-notes": "https://github.com/yogevbd/github-release-notes/tarball/e601b3dba72dcd6cba323c1286ea6dd0c0110b58",
64-
"husky": "4.2.5",
64+
6565
"identity-obj-proxy": "3.0.0",
6666
"jest": "^29.6.3",
6767
"lint-staged": "10.2.11",
@@ -85,11 +85,7 @@
8585
"typedoc": "0.x.x",
8686
"typescript": "^5.8.3"
8787
},
88-
"husky": {
89-
"hooks": {
90-
"pre-commit": "lint-staged"
91-
}
92-
},
88+
9389
"lint-staged": {
9490
"*.{js,ts,tsx}": "eslint --fix",
9591
"*.{h,m,mm}": "node ./scripts/check-clang-format"

0 commit comments

Comments
 (0)