根据我的经验, react-native-webview很难在 iOS 上按我想要的方式运行。当显示键盘并且其内容被键盘隐藏时,它不会自动更改其高度。
它的行为也很奇怪,被KeyboardAvoidingView包裹了。就我而言,它似乎将 WebView 的内容调整得太多,大约是键盘高度的两倍。当我手动监听键盘打开/关闭事件并相应地调整 WebView 的高度时,出现了同样的行为:
componentDidMount(){
Keyboard.addListener("keyboardWillShow", this.keyboardDidShow.bind(this));
Keyboard.addListener("keyboardWillHide", this.keyboardDidHide.bind(this));
}
componentWillUnmount(){
Keyboard.removeListener("keyboardWillShow", this.keyboardDidShow.bind(this));
Keyboard.removeListener("keyboardWillHide", this.keyboardDidHide.bind(this));
}
keyboardDidShow(event){
this.setState({
keyboardHeight: event.endCoordinates.height
});
}
keyboardDidHide(event){
this.setState({
keyboardHeight: 0
});
}
render(){
return (
<WebView
style={{flex: 1, maxHeight: Dimensions.get("window").height - this.state.keyboardHeight}}
/>
);
}
我找到了一个解决方案,不是最优的,但也是一个解决方案。我的答案贴在下面。