Skip to content

Commit dae80ab

Browse files
authored
Merge pull request #714 from callstack/feat/@okwasniewski/fix-updates
fix: multiple updates of frame and contentSize
2 parents a3e4dd9 + 6c57bd3 commit dae80ab

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

ios/RNCPagerView.m

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ - (instancetype)initWithEventDispatcher:(id<RCTEventDispatcherProtocol>)eventDis
4343
- (void)didMoveToWindow {
4444
// Disable scroll view pan gesture for navigation controller screen edge go back gesture
4545
if (self.reactViewController.navigationController != nil && self.reactViewController.navigationController.interactivePopGestureRecognizer != nil) {
46-
[self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.reactViewController.navigationController.interactivePopGestureRecognizer];
47-
}
46+
[self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.reactViewController.navigationController.interactivePopGestureRecognizer];
47+
}
4848
}
4949

50-
- (void)layoutSubviews {
51-
[super layoutSubviews];
52-
[self calculateContentSize];
50+
- (void)didUpdateReactSubviews {
51+
[self updateContentSizeIfNeeded];
5352
}
5453

55-
- (void)didUpdateReactSubviews {
56-
[self calculateContentSize];
54+
- (void)reactSetFrame:(CGRect)frame {
55+
[super reactSetFrame:frame];
56+
[self updateContentSizeIfNeeded];
5757
}
5858

5959
-(void) insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex {
@@ -104,29 +104,23 @@ - (void)shouldDismissKeyboard:(NSString *)dismissKeyboard {
104104

105105
#pragma mark - Internal methods
106106

107-
- (void) calculateContentSize {
108-
UIView *initialView = self.containerView.subviews.firstObject;
109-
if (!initialView) {
110-
return;
111-
}
112-
113-
CGFloat totalSubviewsWidth = initialView.frame.size.width * self.containerView.subviews.count;
114-
CGFloat totalSubviewsHeight = initialView.frame.size.height * self.containerView.subviews.count;
115-
116-
117-
if ([self isHorizontal]) {
118-
_scrollView.contentSize = CGSizeMake(totalSubviewsWidth, 0);
119-
_containerView.frame = CGRectMake(0, 0, totalSubviewsWidth, initialView.bounds.size.height);
120-
} else {
121-
_scrollView.contentSize = CGSizeMake(0, totalSubviewsHeight);
122-
_containerView.frame = CGRectMake(0, 0, initialView.bounds.size.width, totalSubviewsHeight);
123-
}
124-
125-
_scrollView.frame = self.bounds;
126-
[self.scrollView layoutIfNeeded];
127-
128-
if (self.initialPage != 0) {
129-
[self goTo:self.initialPage animated:false];
107+
- (CGSize)contentSize
108+
{
109+
UIView *initialView = _containerView.subviews.firstObject;
110+
CGFloat subviewsCount = _containerView.subviews.count;
111+
return CGSizeMake(initialView.bounds.size.width * subviewsCount, initialView.bounds.size.height * subviewsCount);
112+
}
113+
114+
- (void)updateContentSizeIfNeeded
115+
{
116+
CGSize contentSize = self.contentSize;
117+
if (!CGSizeEqualToSize(_scrollView.contentSize, contentSize)){
118+
_scrollView.contentSize = [self isHorizontal] ? CGSizeMake(contentSize.width, 0) : CGSizeMake(0, contentSize.height);
119+
_containerView.frame = CGRectMake(0, 0, contentSize.width, contentSize.height);
120+
121+
if (self.initialPage != 0) {
122+
[self goTo:self.initialPage animated:false];
123+
}
130124
}
131125
}
132126

@@ -140,7 +134,7 @@ - (void)enableSwipe {
140134

141135
- (void)goTo:(NSInteger)index animated:(BOOL)animated {
142136
CGPoint targetOffset = [self isHorizontal] ? CGPointMake(_scrollView.frame.size.width * index, 0) : CGPointMake(0, _scrollView.frame.size.height * index);
143-
137+
144138
if (animated) {
145139
self.animating = true;
146140
}

0 commit comments

Comments
 (0)