Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dot indicator location on iOS #7274

Merged
merged 5 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/ios/RNNDotIndicatorPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ - (void)apply:(UIViewController *)child options:(DotIndicatorOptions *)options {
if ([self hasIndicator:child])
[self remove:child];

UITabBarController *bottomTabs = [self getTabBarController:child];
int index = (int)[[bottomTabs childViewControllers] indexOfObject:child];
UIView *tabIcon = [bottomTabs getTabIcon:index];

if (!tabIcon) {
return;
}

UIView *indicator = [self createIndicator:options];
[child tabBarItem].tag = indicator.tag;

UITabBarController *bottomTabs = [self getTabBarController:child];
int index = (int)[[bottomTabs childViewControllers] indexOfObject:child];
[[bottomTabs getTabView:index] addSubview:indicator];
[tabIcon addSubview:indicator];
[self applyConstraints:options badge:indicator tabBar:bottomTabs index:index];
}

Expand Down Expand Up @@ -85,14 +91,14 @@ - (BOOL)currentIndicatorEquals:(UIViewController *)child options:(DotIndicatorOp
if (![self hasIndicator:child])
return NO;
UIView *currentIndicator = [self getCurrentIndicator:child];
return
[[currentIndicator backgroundColor] isEqual:[options.color withDefault:[UIColor redColor]]];

return [[currentIndicator backgroundColor] isEqual:[options.color withDefault:[UIColor redColor]]];
}

- (UIView *)getCurrentIndicator:(UIViewController *)child {
UITabBarController *bottomTabs = [self getTabBarController:child];
int tabIndex = (int)[[bottomTabs childViewControllers] indexOfObject:child];
return [[bottomTabs getTabView:tabIndex] viewWithTag:[child tabBarItem].tag];
return [[bottomTabs getTabIcon:tabIndex] viewWithTag:[child tabBarItem].tag];
}

- (BOOL)hasIndicator:(UIViewController *)child {
Expand Down
10 changes: 5 additions & 5 deletions playground/ios/NavigationTests/RNNDotIndicatorPresenterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ - (void)testApply_itDoesNotRecreateIfEqualToCurrentlyVisibleIndicator {
- (void)testApply_itAddsIndicatorToCorrectTabView {
[self applyIndicator];
UIView *indicator1 = [self getIndicator];
XCTAssertEqualObjects([indicator1 superview], [_bottomTabs getTabView:0]);
XCTAssertEqualObjects([indicator1 superview], [_bottomTabs getTabIcon:0]);
}

- (void)testApply_itRemovesPreviousDotIndicator {
NSUInteger childCountBeforeApplyingIndicator = [[_bottomTabs getTabView:0] subviews].count;
NSUInteger childCountBeforeApplyingIndicator = [[_bottomTabs getTabIcon:0] subviews].count;
[self applyIndicator];
NSUInteger childCountAfterApplyingIndicatorOnce = [[_bottomTabs getTabView:0] subviews].count;
NSUInteger childCountAfterApplyingIndicatorOnce = [[_bottomTabs getTabIcon:0] subviews].count;
XCTAssertEqual(childCountBeforeApplyingIndicator + 1, childCountAfterApplyingIndicatorOnce);

[self applyIndicator:[UIColor greenColor]];
NSUInteger childCountAfterApplyingIndicatorTwice = [[_bottomTabs getTabView:0] subviews].count;
NSUInteger childCountAfterApplyingIndicatorTwice = [[_bottomTabs getTabIcon:0] subviews].count;
XCTAssertEqual([[self getIndicator] backgroundColor], [UIColor greenColor]);
XCTAssertEqual(childCountAfterApplyingIndicatorOnce, childCountAfterApplyingIndicatorTwice);
}
Expand All @@ -112,7 +112,7 @@ - (void)testApply_indicatorIsAlignedToTopRightOfIcon {
UIView *indicator = [self getIndicator];
UIView *icon = [_bottomTabs getTabIcon:0];

NSArray<NSLayoutConstraint *> *alignmentConstraints = [_bottomTabs getTabView:0].constraints;
NSArray<NSLayoutConstraint *> *alignmentConstraints = [_bottomTabs getTabIcon:0].constraints;
XCTAssertEqual([alignmentConstraints count], 2);
XCTAssertEqual([alignmentConstraints[0] constant], -4);
XCTAssertEqual([alignmentConstraints[0] firstItem], indicator);
Expand Down