From 15d2c8ac09db49242cc906809e4f0e8e43cd6dd3 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Wed, 25 Dec 2019 14:20:27 +0200 Subject: [PATCH] Fixed scrollview inset when RN view is embedded in another view This happened to me mainly in modal view controllers on iOS 13, where the view controller is floating and does not start from the top. --- .../Keyboard/KeyboardAvoidingView.js | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/Keyboard/KeyboardAvoidingView.js b/Libraries/Components/Keyboard/KeyboardAvoidingView.js index 7c794ea5e8dc..061a21bee21f 100644 --- a/Libraries/Components/Keyboard/KeyboardAvoidingView.js +++ b/Libraries/Components/Keyboard/KeyboardAvoidingView.js @@ -118,7 +118,28 @@ class KeyboardAvoidingView extends React.Component { _onLayout = (event: ViewLayoutEvent) => { this._frame = event.nativeEvent.layout; - if (!this._initialFrameHeight) { + + let isInitial = !this._initialFrameHeight; + + if (this.viewRef.current !== null) { + // Try to measure inside the window, not the view controller + this.viewRef.current.measureInWindow((x, y, width, height) => { + const frame: ViewLayout = { + x: x, + y: y, + width: width, + height: height, + }; + this._frame = frame; + + if (isInitial) { + // save the initial frame height, before the keyboard is visible + this._initialFrameHeight = frame.height; + } + }); + } + + if (isInitial) { // save the initial frame height, before the keyboard is visible this._initialFrameHeight = this._frame.height; }