Jus*_*ead 9 platform react-native
我想只使用KeyboardAwareScrollView而不在IOS上使用任何功能,并为Android提供以下代码。
我知道我需要使用Platform.OS ==='ios'吗?:
但是我不了解如何实现它。请帮我
render(){
return(
<KeyboardAwareScrollView
extraScrollHeight={100}
enableOnAndroid={true}
keyboardShouldPersistTaps='handled'
>
<TextInput
style={styles.inputContainerStyle}
label="Username"
value={this.state.username}
onChangeText={username => this.setState({ username })}
/>
</KeyboardAwareScrollView>
)
}
Run Code Online (Sandbox Code Playgroud)
我在下面尝试过的方法:(但是不起作用)
<KeyboardAwareScrollView
Platform.OS === 'android' ?
(
extraScrollHeight={100}
enableOnAndroid={true}
keyboardShouldPersistTaps='handled'
) : null
>
Run Code Online (Sandbox Code Playgroud)
您也可以通过节点检查,但是react-native提供了一些检查平台的方法。
推荐这个
import {Platform} from 'react-native';
st styles = StyleSheet.create({
container: {
flex: 1,
...Platform.select({
ios: {
backgroundColor: 'red',
},
android: {
backgroundColor: 'blue',
},
}),
},
});
Run Code Online (Sandbox Code Playgroud)
您还可以使用三元表达式
import {Platform, StyleSheet} from 'react-native';
const styles = StyleSheet.create({
height: Platform.OS === 'ios' ? 200 : 100,
});
Run Code Online (Sandbox Code Playgroud)
您可以同时使用JSX prop扩展和三元表达式:
<KeyboardAwareScrollView
{
...(Platform.OS === 'android' ?
{
extraScrollHeight={100}
enableOnAndroid={true}
keyboardShouldPersistTaps='handled'
} : {})
}
>
Run Code Online (Sandbox Code Playgroud)
但我强烈建议您不要全部内联执行此操作,因为这很难阅读。您可以使用以下方法进行一些可读性(声明性)的操作Platform.select:
const keyboardProps = Platform.select({
android: { … },
ios: {},
});
…
<KeyboardAwareScrollView {...keyboardProps}>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16504 次 |
| 最近记录: |