Skip to content

Commit

Permalink
feat: add support for customizable scroll indicators in PdfView compo…
Browse files Browse the repository at this point in the history
…nent (#904)

- Added `showsHorizontalScrollIndicator` and `showsVerticalScrollIndicator` props to PdfView.js for better user control over scroll indicators.
- Refactored RNPDFPdfView.mm to streamline the handling of scroll indicator visibility using a new `setScrollIndicators` method, improving code maintainability.

Co-authored-by: liangyuxin <[email protected]>
  • Loading branch information
fjlyx97 and xinyucoding authored Feb 4, 2025
1 parent cca0e99 commit b6475f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
4 changes: 4 additions & 0 deletions PdfView.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export default class PdfView extends Component {
singlePage: PropTypes.bool,
onPageSingleTap: PropTypes.func,
onScaleChanged: PropTypes.func,
showsHorizontalScrollIndicator: PropTypes.bool,
showsVerticalScrollIndicator: PropTypes.bool,
};

static defaultProps = {
Expand All @@ -62,6 +64,8 @@ export default class PdfView extends Component {
},
onScaleChanged: (scale) => {
},
showsHorizontalScrollIndicator: true,
showsVerticalScrollIndicator: true,
};

constructor(props) {
Expand Down
53 changes: 19 additions & 34 deletions ios/RNPDFPdf/RNPDFPdfView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -479,40 +479,8 @@ - (void)didSetProps:(NSArray<NSString *> *)changedProps
}
}

if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"showsHorizontalScrollIndicator"])) {
if (_showsHorizontalScrollIndicator) {
for (UIView *subview in _pdfView.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
scrollView.showsHorizontalScrollIndicator = YES;
}
}
} else {
for (UIView *subview in _pdfView.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
scrollView.showsHorizontalScrollIndicator = NO;
}
}
}
}

if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"showsVerticalScrollIndicator"])) {
if (_showsVerticalScrollIndicator) {
for (UIView *subview in _pdfView.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
scrollView.showsVerticalScrollIndicator = YES;
}
}
} else {
for (UIView *subview in _pdfView.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
scrollView.showsVerticalScrollIndicator = NO;
}
}
}
if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"showsHorizontalScrollIndicator"] || [changedProps containsObject:@"showsVerticalScrollIndicator"])) {
[self setScrollIndicators:self horizontal:_showsHorizontalScrollIndicator vertical:_showsVerticalScrollIndicator depth:0];
}

if (_pdfDocument && ([changedProps containsObject:@"path"] || [changedProps containsObject:@"scrollEnabled"])) {
Expand Down Expand Up @@ -928,6 +896,23 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecogni
return !_singlePage;
}

- (void)setScrollIndicators:(UIView *)view horizontal:(BOOL)horizontal vertical:(BOOL)vertical depth:(int)depth {
// max depth, prevent infinite loop
if (depth > 10) {
return;
}

if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)view;
scrollView.showsHorizontalScrollIndicator = horizontal;
scrollView.showsVerticalScrollIndicator = vertical;
}

for (UIView *subview in view.subviews) {
[self setScrollIndicators:subview horizontal:horizontal vertical:vertical depth:depth + 1];
}
}

@end

#ifdef RCT_NEW_ARCH_ENABLED
Expand Down

0 comments on commit b6475f8

Please sign in to comment.