Skip to content

Commit 7fbc9c8

Browse files
troZeedratwas
authored andcommitted
fix: position of dots indicator (#95)
* fix: add patch to react package * fix: position of page control * update example * rename state property
1 parent 22ab617 commit 7fbc9c8

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

example/ViewPagerExample.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type State = {
3535
},
3636
pages: Array<CreatePage>,
3737
scrollState: PageScrollState,
38+
dotsVisible: boolean,
3839
};
3940

4041
export default class ViewPagerExample extends React.Component<*, State> {
@@ -58,6 +59,7 @@ export default class ViewPagerExample extends React.Component<*, State> {
5859
},
5960
pages: pages,
6061
scrollState: 'idle',
62+
dotsVisible: false,
6163
};
6264
this.viewPager = React.createRef();
6365
}
@@ -110,8 +112,12 @@ export default class ViewPagerExample extends React.Component<*, State> {
110112
);
111113
}
112114

115+
toggleDotsVisibility = () => {
116+
this.setState(prevState => ({dotsVisible: !prevState.dotsVisible}));
117+
};
118+
113119
render() {
114-
const {page, pages, animationsAreEnabled} = this.state;
120+
const {page, pages, animationsAreEnabled, dotsVisible} = this.state;
115121
return (
116122
<SafeAreaView style={styles.container}>
117123
<ViewPager
@@ -126,7 +132,7 @@ export default class ViewPagerExample extends React.Component<*, State> {
126132
orientation="horizontal"
127133
// Lib does not support dynamically transitionStyle change
128134
transitionStyle="scroll"
129-
showPageIndicator
135+
showPageIndicator={dotsVisible}
130136
ref={this.viewPager}>
131137
{pages.map(p => this.renderPage(p))}
132138
</ViewPager>
@@ -140,6 +146,11 @@ export default class ViewPagerExample extends React.Component<*, State> {
140146
this.setState({scrollEnabled: !this.state.scrollEnabled})
141147
}
142148
/>
149+
<Button
150+
enabled={true}
151+
text={dotsVisible ? 'Hide dots' : 'Show dots'}
152+
onPress={this.toggleDotsVisibility}
153+
/>
143154
<Button enabled={true} text="Add new page" onPress={this.addPage} />
144155
</View>
145156
<View style={styles.buttons}>

ios/ReactNativePageView.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,8 @@ - (void)embed {
292292
[self renderChildrenViewControllers];
293293
_reactPageIndicatorView = [self createPageIndicator:self];
294294
_reactPageIndicatorView.hidden = !_showPageIndicator;
295-
295+
296296
[[self reactViewController] addChildViewController:_reactPageViewController];
297-
298297
[reactPageViewController.view addSubview:_reactPageIndicatorView];
299298
[self addSubview:reactPageViewController.view];
300299
_reactPageViewController.view.frame = [self bounds];
@@ -303,6 +302,12 @@ - (void)embed {
303302

304303
// Add the page view controller's gesture recognizers to the view controller's view so that the gestures are started more easily.
305304
self.gestureRecognizers = _reactPageViewController.gestureRecognizers;
305+
_reactPageIndicatorView.translatesAutoresizingMaskIntoConstraints = NO;
306+
NSLayoutConstraint *bottomConstraint = [_reactPageIndicatorView.bottomAnchor constraintEqualToAnchor: self.reactPageViewController.view.bottomAnchor constant:0];
307+
NSLayoutConstraint *leadingConstraint = [_reactPageIndicatorView.leadingAnchor constraintEqualToAnchor: self.reactPageViewController.view.leadingAnchor constant:0];
308+
NSLayoutConstraint *trailingConstraint = [_reactPageIndicatorView.trailingAnchor constraintEqualToAnchor: self.reactPageViewController.view.trailingAnchor constant:0];
309+
[self.reactPageViewController.view addConstraints:@[bottomConstraint,leadingConstraint,trailingConstraint]];
310+
[self.reactPageViewController.view layoutIfNeeded];
306311
} else {
307312
RCTLog(@"getParentViewController returns nil");
308313
}
@@ -473,11 +478,7 @@ - (void)shouldShowPageIndicator:(BOOL)showPageIndicator {
473478
- (UIPageControl *)createPageIndicator:(UIView *)parentView {
474479
CGPoint parentOrigin = parentView.frame.origin;
475480
CGSize parentSize = parentView.frame.size;
476-
UIPageControl *pageControl = [[UIPageControl alloc]
477-
initWithFrame:(CGRectMake(parentOrigin.x,
478-
parentSize.height - 70,
479-
parentSize.width,
480-
70))];
481+
UIPageControl *pageControl = [[UIPageControl alloc] init];
481482
pageControl.numberOfPages = _childrenViewControllers.count;
482483
pageControl.currentPage = _initialPage;
483484
pageControl.tintColor = UIColor.blackColor;

0 commit comments

Comments
 (0)