React Native ios选择器始终打开

时间:2016-12-16 09:58:45

标签: ios react-native picker

我的屏幕上有两个选择器。每当我在iOS应用程序中导航到屏幕时,我发现拾取器始终处于打开状态,所有选项都可见。

enter image description here

在Android中完全正常,只有在我们点击选择器后才能看到选项。

有人可以提出解决方案来解决这个问题吗?

8 个答案:

答案 0 :(得分:14)

在iOS上使用ActionSheet而不是Picker。 https://facebook.github.io/react-native/docs/actionsheetios.html

正如jevakallio所回答,这是iOS上的默认行为。但这并不能提供良好的用户体验,因此请删除所有选择器组件并替换为ActionSheet。

我做了,效果很好。之所以我更喜欢ActionSheet而不是jevakallio建议的其他组件,因为它是由RN团队开发的,具有良好的本土感觉。建议的最后一个选项react-native-modal-picker也非常好。

enter image description here

答案 1 :(得分:10)

这就是iOS UIPickerView组件的工作方式 - 没有办法自定义它。

如果你想要一种不同类型的UI元素,你需要编写自己的UI元素,或者使用众多开源库中的一个,例如:

使用这些和类似的关键字搜索,也会产生许多其他库。

答案 2 :(得分:6)

React-native-modal-picker已停止使用。 react-native-modal-selector

答案 3 :(得分:1)

我不知道您为什么选择ActionSheet作为可接受答案的答案。 但是,我将提供解决此问题的方法:

将此值设置为您的状态:

this.state= {
    pickerOpacity: 0,
    opacityOfOtherItems: 1 //THIS IS THE OPACITY OF ALL OTHER ITEMS, WHICH COLLIDES WITH YOUR PICKER.
    label: 'Firstvalue'
}

在渲染方法中,请执行以下操作:

{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>

所以现在我们要检查客户端是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
             });
          }
     }
}

答案 4 :(得分:1)

react-native-dropdown具有参考错误 如果您不想使用“动作表”而是一个“真正的”选择器,那么我发现这个选择器会更好...

https://github.com/lawnstarter/react-native-picker-select

答案 5 :(得分:0)

在扩展ActionSheetIOS的基础上,我创建了一个自定义组件,该组件可以直接替代Picker(我正在使用https://react-native-elements.github.io/react-native-elements/docs/overview.html中的Button):

import React from 'react';
import { ActionSheetIOS, Platform } from 'react-native';
import { Button } from 'react-native-elements';

class PickerDropDown extends React.Component {
  onIOSButton = () => {
    let options = this.props.children.map((item, i) => {
      return item.props.label;
    });
    options.push("Cancel");
    ActionSheetIOS.showActionSheetWithOptions(
      {
        options: options,
        cancelButtonIndex: options.length - 1,
      },
      this.onIOSButtonPick
    );
  }

  onIOSButtonPick = (buttonIndex) => {
    if (buttonIndex < this.props.children.length && buttonIndex != this.props.selectedValue) {
      if (typeof this.props.selectedValue === 'undefined' || (typeof this.props.selectedValue !== 'undefined' && buttonIndex != this.findIndexForValue(this.props.selectedValue))) {
        this.props.onValueChange(this.props.children[buttonIndex].props.value, buttonIndex);
      }
    }
  }

  findLabelForValue = (searchValue) => {
    for (let i = 0; i < this.props.children.length; i++) {
      if (this.props.children[i].props.value == searchValue) {
        return this.props.children[i].props.label;
      }
    }
    return null;
  }

  findIndexForValue = (searchValue) => {
    for (let i = 0; i < this.props.children.length; i++) {
      if (this.props.children[i].props.value == searchValue) {
        return i;
      }
    }
    return -1;
  }

  render() {
    if (Platform.OS === "ios") {
      let title = "";
      if (this.props.children && this.props.children.length > 0) {
        if (typeof this.props.selectedValue !== 'undefined') {
          title = this.findLabelForValue(this.props.selectedValue);
        } else {
          title = this.props.children[0].props.label;
        }
      }
      return (
        <Button
          title={title}
          onPress={this.onIOSButton}
          type="clear"
          icon={{
            name: "arrow-drop-down",
            size: 15,
            color: "black"
          }}
          iconRight={true}
        />
      );
    } else {
      return (
        <Picker {...this.props} />
      );
    }
  }
}

答案 6 :(得分:0)

使用

<Picker itemStyle={{height:50}} > </Picker>

它仅影响ios选择器。将选择器项目样式的高度更改为标准高度。

答案 7 :(得分:-2)

native-base库而不是react-native导入