Goal
On mobile Safari, when the virtual keyboard is open, the screen should render like this image:
where:
- The navbar and the input are fixed in place
- The list of text messages is scrollable
Problem
On mobile Safari, when the soft keyboard is open, dragging on the input or navbar can move the entire viewport up and down.
I have a screen capture video demonstrating the problem: https://youtu.be/GStBjRVpoGU
I've spent months on this problem, including trawling through many similar questions on Stack Overflow, but I've been unable to find a solution that works (or, at least, a solution that I've been able to make work).
Background
This app is a hybrid mobile app: built as a web app with React.js, but wrapped in a React Native app using the WebView component. Nevertheless, the same problem exists even if the web app is opened in a normal mobile Safari window.
Mobile Safari has a related problem where the soft keyboard pushes the whole viewport up when it is opened, so that the top half of the view port is pushed up and off the screen. This excellent blog provides both and description of that problem and a solution for it. I implemented that solution. It stopped the viewport from moving up when the soft keyboard opens, but the viewport can still slide around after the soft keyboard opens.
Another problem is that mobile Safari doesn't update window.innerHeight
when the soft keyboard is opened/closed. To get around this, I used react-native-keyboard-spacer
in the React Native app, a bit like this:
render() {
return (
<React.Fragment>
<SafeAreaView >
<WebView/>
</SafeAreaView>
<KeyboardSpacer />
</React.Fragment>
);
}
This changes the height of the Webview whenever the soft keyboard opens/closes, and thus window.innerHeight
etc also changes.
It's also known that position: fixed;
on mobile Safari doesn't work so well when the soft keyboard is open, so I used position: absolute;
instead, thanks to the suggestions at this other very useful blog.
I created a code sandbox to demonstrate the problem. Open it in mobile Safari to see the screen slide after the virtual keyboard is opened. You can also the actual code sandbox code here, which represents the closest I've come to solving this problem.
One thing to note about the code sandbox, though. There’s no WebView or KeyboardSpacer: it’s just a web page. As such, I’ve had to hardcode some heights in. But if you open it in mobile Safari you’ll see the viewport sliding all over the place once the soft keyboard is open.
Has anybody seen this particular problem before? How did you fix it? Many thanks in advance.