From 301830dc2a76fe30b795b5e12fedfcc620328e72 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 18 Jul 2017 14:33:51 -0700 Subject: [PATCH] ScrollView: Use autoresizing masks for layouting actual UIScrollView Summary: Surprisingly enough, even if semantically the code remains identical, layouting via autoresizing masks applies changes to subviews a bit earlier than iOS calls `layoutSubviews`. This allows us to avoid situations where we already explicitly set calculated by Yoga frames and want to scroll to some subview, but actual layout have not done yet and internal views has wrong frames. Reviewed By: javache Differential Revision: D5414440 fbshipit-source-id: d4152c9c68dc17f6827832dcb45e5ba86fb82831 --- React/Views/RCTScrollView.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/React/Views/RCTScrollView.m b/React/Views/RCTScrollView.m index 24d129ba835e0c..2653fc143c90e3 100644 --- a/React/Views/RCTScrollView.m +++ b/React/Views/RCTScrollView.m @@ -325,7 +325,10 @@ - (void)setFrame:(CGRect)frame return; } + // Preserving `contentOffset` between layout passes. + CGPoint originalOffset = self.contentOffset; [super setFrame:frame]; + self.contentOffset = originalOffset; } #if !TARGET_OS_TV @@ -362,6 +365,7 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher if ((self = [super initWithFrame:CGRectZero])) { _eventDispatcher = eventDispatcher; _scrollView = [[RCTCustomScrollView alloc] initWithFrame:CGRectZero]; + _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; _scrollView.delegate = self; _scrollView.delaysContentTouches = NO; _automaticallyAdjustContentInsets = YES; @@ -466,10 +470,6 @@ - (void)layoutSubviews RCTAssert(self.subviews.count == 1, @"we should only have exactly one subview"); RCTAssert([self.subviews lastObject] == _scrollView, @"our only subview should be a scrollview"); - CGPoint originalOffset = _scrollView.contentOffset; - _scrollView.frame = self.bounds; - _scrollView.contentOffset = originalOffset; - #if !TARGET_OS_TV // Adjust the refresh control frame if the scrollview layout changes. RCTRefreshControl *refreshControl = _scrollView.rctRefreshControl;