简体   繁体   English

如何使 iOS UIPicker 与多个列和标题反应原生?

[英]How do I make an iOS UIPicker in react native with multiple columns and titles?

Let's pretend my problem is I want a user to be able to select an amount of apples, and an amount of pears, with one control.让我们假设我的问题是我希望用户能够通过一个控件选择一定数量的苹果和一定数量的梨。 I see the picker in the "Timer" section of the clock app bundled with iOS, and I like it.我在 iOS 捆绑的时钟应用程序的“计时器”部分看到了选择器,我喜欢它。

iOS 计时器选择器

I want exactly that, except instead of three columns, I want two, and instead of "hours" and "min", I want "apples" and "pears".我完全想要那个,除了不是三列,我想要两列,而不是“小时”和“分钟”,我想要“苹果”和“梨”。

So far, I'm able to put two pickers next to each other, which, while not curving the items towards each other slightly as if they were on the same wheel, is good enough for me for my columns problem for now.到目前为止,我能够将两个选择器并排放置,虽然它们不会像在同一个轮子上那样略微弯曲物品,但对于我现在的列问题来说已经足够了。 I'm as yet unable to put titles on the rows, though.不过,我还无法在行上放置标题。

Here's what I have:这是我所拥有的:

  render() {
    let PickerIOSItem = PickerIOS.Item
    return (
      <View style={styles.container}>
        <PickerIOS style={styles.column}>
          <PickerIOSItem value={1} label="0" />
          <PickerIOSItem value={2} label="1" />
          <PickerIOSItem value={3} label="2" />
          <PickerIOSItem value={4} label="3" />
          <PickerIOSItem value={5} label="4" />
        </PickerIOS>
        <PickerIOS style={styles.column}>
          <PickerIOSItem value={1} label="0" />
          <PickerIOSItem value={2} label="1" />
          <PickerIOSItem value={3} label="2" />
          <PickerIOSItem value={4} label="3" />
          <PickerIOSItem value={5} label="4" />
        </PickerIOS>
      </View>
    );
  }

styles.container has display: flex and flex-direction: row , and styles.column has width: 49% . styles.containerdisplay: flexflex-direction: row ,而styles.columnwidth: 49%

我的垃圾尝试

Wrap it in a <View> and use <Text> with flex:1 should work将它包裹在<View>并使用<Text>flex:1应该可以工作

<View style={{ display: 'flex', flexDirection: "row" }}>
  <View style={{ flex: 1, flexDirection: 'row',alignItems: 'center' }}>
    <PickerIOS style={{ flex: 1 }}>
      <PickerIOSItem value={1} label="0" />
      <PickerIOSItem value={2} label="1" />
      <PickerIOSItem value={3} label="2" />
      <PickerIOSItem value={4} label="3" />
      <PickerIOSItem value={5} label="4" />
    </PickerIOS>
    <Text>Apples</Text>
  </View>
  <View style={{ flex: 1, flexDirection: 'row',alignItems: 'center' }}>
    <PickerIOS style={{ flex: 1 }}>
      <PickerIOSItem value={1} label="0" />
      <PickerIOSItem value={2} label="1" />
      <PickerIOSItem value={3} label="2" />
      <PickerIOSItem value={4} label="3" />
      <PickerIOSItem value={5} label="4" />
    </PickerIOS>
    <Text>pears</Text>
  </View>
</View>

Output will be something like below输出将如下所示

在此处输入图片说明

Though I have not focused on design, you can make it according to your need.虽然我没有专注于设计,但您可以根据需要进行设计。

我最近制作了一个支持多列的跨平台“Wheel Picker”库: https : //www.npmjs.com/package/react-native-segmented-picker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM