OGeek|极客世界-中国程序员成长平台

标题: ios - react 原生 View 上的选择器放置错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:55
标题: ios - react 原生 View 上的选择器放置错误

我正在关注 Udemy react-native course在其中一个示例中,他使用选择器来选择屏幕中的数据。当他尝试它时,它似乎正在工作,但现在当我尝试渲染它时,我得到了一个奇怪的结果。

如果我完全按照他的代码,选择器显示在所有其他项目之后,在进行一些更改后,我让它显示在正确的位置,但现在它被压扁了,这仍然不正确:

bad pickler

在如何渲染它方面我肯定做错了,这里是代码(full example on github):

import React from 'react';
import {Picker, Text, StyleSheet, View} from 'react-native';
import {connect} from 'react-redux';
import {Card, CardSection, Input, Button} from "./common";
import {employeeUpdate} from "../actions";

class EmployeeCreate extends React.Component {

  updateEmployee(name, value) {
    this.props.employeeUpdate({prop: name, value: value})
  }

  renderPickerItems() {
    return ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
      .map((item) => <icker.Item key={item} label={item} value={item}/>);
  }

  render() {
    return (
      <Card>

        <CardSection>
          <Input
            label="Name"
            placeholder="Your Name"
            value={this.props.name}
            onChangeText={this.updateEmployee.bind(this, 'name')}
          />
        </CardSection>

        <CardSection>
          <Input
            label="hone"
            placeholder="555-555-5555"
            keyboardType="phone-pad"
            value={this.props.phone}
            onChangeText={this.updateEmployee.bind(this, 'phone')}
          />
        </CardSection>

        <CardSection style={{flex: 1}}>
          <View style={styles.shiftContainerStyle}>
            <Text style={styles.pickerTextStyle}>Shift</Text>
            <icker
              style={styles.pickerStyle}
              selectedValue={this.props.shift}
              onValueChange={this.updateEmployee.bind(this, 'shift')}
            >
              {this.renderPickerItems()}
            </Picker>
          </View>
        </CardSection>

        <CardSection>
          <Button>
            Create
          </Button>
        </CardSection>

      </Card>
    );
  }

}

const styles = StyleSheet.create({
  pickerTextStyle: {
    fontSize: 18,
    lineHeight: 23,
    flex: 1,
  },
  pickerStyle: {
    flex: 2,
  },
  shiftContainerStyle: {
    flex: 1,
    flexDirection: 'row',
    alignItems: 'center',
  }
});

const mapStateToProps = state => {
  const {name, phone, shift} = state.employeeForm;

  return {
    name,
    phone,
    shift,
  };
};

export default connect(mapStateToProps, {employeeUpdate})(EmployeeCreate);

知道我可以做些什么来正确渲染它吗?



Best Answer-推荐答案


您需要从代码的这一行中删除 style={{flex: 1}}:

<CardSection style={{flex: 1}}>

原因是您的父容器 Card 没有定义任何 flexwidth/height 值。如果 flex 未定义,the default is flex: 0 .如果您查看 flex 的文档,你会看到:

When flex is 0, the component is sized according to width and height and it is inflexible.

将其与未定义 width/height 相结合,您会在呈现 CardSections 时获得此行为:

When flex is a positive number, it makes the component flexible and it will be sized proportional to its flex value. So a component with flex set to 2 will take twice the space as a component with flex set to 1.

一旦你删除 style={{flex: 1}}CardSectionwidthheight > 将由它的子组件定义,例如 InputButton,确实有样式和默认样式。

根据 Yoga 这是否是正确的行为规范(Yoga 是 React Native 用于布局的)适用于 debate并且有 tripped up别人之前。一定要看看 first StackOverflow answer我链接到,因为它比任何关于 React Native wrt flex 的文档都更详细和解释了陷阱。

关于ios - react 原生 View 上的选择器放置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49164441/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4