Mol*_*per 14 javascript react-native
我正在使用React Native的Keyboard Avoiding View,行为设置为padding(在Android上测试).
我的屏幕上有多个TextInputs.当我单击最终的TextInput时,键盘会覆盖它.我现在能够向下滚动,因为从KeyboardAvoidingView添加了填充,但它是理想的自动滚动焦点.
<Content>
<KeyboardAvoidingView behavior='padding'>
<TextInput placeholder='Example 1' />
<TextInput placeholder='Example 2' />
<TextInput placeholder='Example 3' />
<TextInput placeholder='Example 4' />
<TextInput placeholder='Example 5' />
<TextInput placeholder='Example 6' />
<TextInput placeholder='Example 7' />
</KeyboardAvoidingView>
</Content>
Run Code Online (Sandbox Code Playgroud)
far*_*nd2 20
有一个名为keyboardVerticalOffset的道具,您可以传递给KeyboardAvoidingView,它将改变键盘移过textInput的程度.我的代码示例:
const keyboardVerticalOffset = Platform.OS === 'ios' ? 40 : 0
return (
<KeyboardAvoidingView behavior='position' keyboardVerticalOffset={keyboardVerticalOffset}>
<ListView .../>
<KeyboardAvoidingView/>
)
Run Code Online (Sandbox Code Playgroud)
Flo*_*bre 10
react-native-keyboard-aware-scroll-view
我发现它使用起来非常简单,而且在 Android 和 iOS 上都运行良好。
它也支持旧版本的 RN。
最初我尝试过KeyboardAvoidingView但在 IOS 上甚至没有
behavior='position'与keyboardVerticalOffset工作正常。
他们过去常常以一种奇怪的方式重叠一些内容。
我有:
RN 0.53.3
react-native-keyboard-aware-scroll-view 0.6.0
Run Code Online (Sandbox Code Playgroud)
我在此处添加了有关我的用例的更多详细信息:
小智 7
要添加@Maxwell的答案,有时您可能需要滚动到比滚动视图末尾更远的位置才能将组件放入视图中,因为添加的填充不是键盘的完整高度。下面的完整示例使用scrollTo(),其中 y 偏移量作为文本输入的高度。
import React, { Component } from 'react'
import {
KeyboardAvoidingView,
ScrollView,
View,
TextInput
} from 'react-native'
export default class Test extends Component {
render() {
return (
<ScrollView style = {{flex:1, backgroundColor: 'white'}} ref = 'scroll'>
<KeyboardAvoidingView behavior='position' style = {{backgroundColor: 'white', flex: 1}}>
<View style = {{height: 400}}/>
<TextInput style = {{height: 60}} placeholder='Example 1' />
<TextInput style = {{height: 60}} placeholder='Example 2' />
<TextInput style = {{height: 60}} placeholder='Example 3' />
<TextInput style = {{height: 60}} placeholder='Example 4' onFocus = {() => this.refs['scroll'].scrollTo({y: 60})}/>
</KeyboardAvoidingView>
</ScrollView>
)
}
}
Run Code Online (Sandbox Code Playgroud)
根据平台,Android或IOS的不同,实现可能略有不同。这是我做的。
在AndroidManifest.xml中添加 android:windowSoftInputMode =“ adjustResize”,
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
</activity>
Run Code Online (Sandbox Code Playgroud)
在你的容器里
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : null}
keyboardVerticalOffset={Platform.OS === "ios" ? 64 : 0}>
<ScrollView>
{...}
</ScrollView>
</KeyboardAvoidingView>
Run Code Online (Sandbox Code Playgroud)
keyboardVerticalOffset表示键盘经过textInput的移动量。
| 归档时间: |
|
| 查看次数: |
25953 次 |
| 最近记录: |