【问题标题】:React Native ios picker is always openReact Native ios 选择器始终打开
【发布时间】:2017-05-02 02:09:51
【问题描述】:

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

它在 Android 中运行得非常好,只有在我们单击选择器后才能看到选项。

有人可以建议在 iOS 中解决此问题的解决方案吗?

【问题讨论】:

  • 如何删除选择器行
  • 您可以,仅适用于 iOS,将所选值显示为可触摸文本,当触摸该文本时,选择器可见。然后在选择一个项目后再次隐藏选择器。

标签: ios react-native picker


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

这就是 iOS UIPickerView 组件的工作原理 - 无法自定义它。

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

使用这些以及类似的关键字进行谷歌搜索也会产生许多其他库。

【讨论】:

  • 有用。但是不能优先使用操作表来处理一长串数据。而在 react-native-modal-picker/react-native-modal-selector 的底部还有一个额外的填充。目前还没有找到完美的解决方案。
【解决方案3】:

我不知道您为什么选择带有 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
             });
          }
     }
}

编辑: iOS 屏幕截图 (Click here for Video)

【讨论】:

  • 你有没有这种世博小吃?我很难实现这一点,我什至不确定它是否有效。
  • 很遗憾没有。您只需在渲染选择器之前放置 toggle() 和 checkIfIOS() 方法并运行 checkIfOs 并在选择器 onValueChange 事件中使用 toggle() 方法。如果你能提供你的观点,可以帮助你如何实现它。
  • @SercanSametSavran 你能提供选择器的屏幕截图吗?按钮在视图中的外观如何?似乎缺少 this.changeOpacity() 的实现
  • 能否给我们IOS截图?
  • 这是迄今为止最好的解决方案,应该是公认的答案。
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    扩展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 (
          <View>
            <Button
              title={title}
              onPress={this.onIOSButton}
              type="clear"
              icon={{
                name: "arrow-drop-down",
                size: 15,
                color: "black"
              }}
              iconRight={true}
            />
         </View>
          );
        } else {
          return (
            <Picker {...this.props} />
          );
        }
      }
    }
    

    【讨论】:

    • 它是什么样子的?请提供视频或快照
    • 我用过这个并且工作过。只是它没有像 Picker 那样使用完整的可用宽度
    • 我将它包裹在 &lt;View&gt; 中,并在其上设置了 style 属性。 react-native-elements 按钮上的样式未应用。
    • 最佳答案。这个解决方案尽可能使用原生选择器,代码很少。并且 dropin 替换可以快速实施。
    • 我使用这个类取得了很好的效果,对样式进行了一些小改动,按照@IsmailS 的建议将其包装在&lt;View&gt; 中,并将其发布到 Gist:gist.github.com/thargenediad/7615e281baab25733ca0e7f5c7cb97d6 我希望它有所帮助有人可以节省尝试设置 Kevin 组件样式的时间。享受吧!
    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      使用

      &lt;Picker itemStyle={{height:50}} &gt; &lt;/Picker&gt;

      它只影响 ios 选择器。将选择器 itemstyle 高度更改为您的标准。

      【讨论】:

        【解决方案8】:

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

        【讨论】:

        • 问题在于反应原生选择器元素,而像 native-base 这样的第三方库不是我们正在寻找的
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        • 2021-05-28
        • 2016-11-07
        • 2022-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多