Abh*_*lin 26 picker ios react-native
我的屏幕上有两个选择器.每当我在iOS应用程序中导航到屏幕时,我发现拾取器始终处于打开状态,并且所有选项都可见.
它在Android中完美运行,只有在我们点击选择器后才能看到选项.
有人可以建议一个解决方案来解决这个问题吗?
Abh*_*lin 17
在iOS上使用ActionSheet而不是Picker. https://facebook.github.io/react-native/docs/actionsheetios.html
正如jevakallio所回答的,这是iOS上的默认行为.但这并不能提供良好的用户体验,因此请删除所有选择器组件并替换为ActionSheet.
我做了,效果很好.我之所以选择ActionSheet而不是jevakallio建议的其他组件,是因为它是由RN团队开发并具有良好的原生感.最后一个选项建议react-native-modal-picker也非常好.
我不知道您为什么选择 ActionSheet 作为已接受的答案。不过我会针对这个问题给出一个解决方法:
将此值放在您的状态中:
this.state= {
pickerOpacity: 0,
opacityOfOtherItems: 1 //THIS IS THE OPACITY OF ALL OTHER ITEMS, WHICH COLLIDES WITH YOUR PICKER.
label: 'Firstvalue'
}
Run Code Online (Sandbox Code Playgroud)
在您的渲染方法中执行以下操作:
{this.checkIfIOS()}
<Picker
selectedValue={this.state.selected}
style={{ height: 50, alignSelf: 'center', opacity: this.state.pickerOpacity, marginBottom:30, width: 250}}
onValueChange={(itemValue, itemIndex) =>{
this.setState({
selected: itemValue,
label: itemValue
});
toggle();
}
}>
<Picker.Item label="Your Label" value="yourValue"/>
</Picker>
Run Code Online (Sandbox Code Playgroud)
所以现在我们要检查一下,我们的客户端是 android 还是 ios。因此导入 Platform 并将 checkIfIos()-Method 放入您的代码中:
import {Platform} from 'react-native'
checkIfIOS(){
if(Platform.OS === 'ios'){ // check if ios
console.log("IOS!!!");
//this button will (onpress) set our picker visible
return (<Button buttonStyle={{backgroundColor:'#D1D1D1', opacity: this.state.opacityOfOtherItems}} onPress={this.toggle()} color="#101010" title={this.state.label} onPress={this.changeOpacity}/>);
}else if(Platform.OS === 'android'){ //check if android
this.setState({
pickerOpacity: 1 //set picker opacity:1 -> picker is visible.
});
console.log("ANDROID!!!");
}
}
toggle(){
if(Platform.OS === 'ios'){
if(this.state.pickerOpacity == 0){
this.setState({
pickerOpacity: 1,
opacityOfOtherItems: 0 // THIS WILL HIDE YOUR BUTTON!
});
}else{
this.setState({
pickerOpacity: 0,
opacityOfOtherItems: 1
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑: iOS 截图(点击此处观看视频)
| 归档时间: |
|
| 查看次数: |
26905 次 |
| 最近记录: |