【问题标题】:KeyboardAvoidingView not working react-nativeKeyboardAvoidingView 无法正常工作
【发布时间】:2021-07-26 16:51:09
【问题描述】:

我正在尝试使用 KeyboardAvoidingView,但我没有得到任何结果。

带有样式组件的样式视图:


export const Container = styled.View({
  backgroundColor: primary,
  flex: 1,
  alignItems: 'center',
});

export const ImageView = styled.View({
  marginTop: 16,
  alignItems: 'center',
  justifyContent: 'center',
});

export const InputView = styled.View({
  width: '100%',
  marginTop: 16,
});

export const TextView = styled.View({
  marginLeft: 42,
  marginRight: 42,
  marginTop: 22,
  alignItems: 'center',
  justifyContent: 'center',
});

export const BottomView = styled.View({
  flex: 1,
  justifyContent: 'flex-end',
  alignItems: 'center',
  width: '100%',
});

这是我的布局。我设置了 behavior = padding 试图解决我的问题,但不起作用。

 <KeyboardAvoidingView behavior="padding" style={{flex: 1}}>
      <Container>
        {load && <Loading />}

        <TextView>
          <Text value={diaryStrings.title} isModalMessage />
        </TextView>
        <TextView>
          <Text value={diaryStrings.hintMessage} isModalMessage />
        </TextView>
        <TextView>
          <Text value={diaryStrings.tipMessage} isModalMessage />
        </TextView>

        <ImageView>
          <ImageSvg name={'calendar'} />
        </ImageView>
        <InputView>
          <Input
            placeholder={diaryStrings.diaryName}
            placeholderTextColor={tertiary}
            maxLength={20}
            iconName="book"
            onChangeText={text => {
              setDiaryNameError('');
              setDiaryName(text);
            }}
            maskType="nomask"
            error={diaryNameError}
            value={diaryName}
          />
          <Input
            placeholder={diaryStrings.initialDate}
            placeholderTextColor={tertiary}
            maxLength={10}
            iconName="date-range"
            onChangeText={text => {
              setDiaryInitDateError('');
              setDiaryInitDate(text);
            }}
            maskType="datetime"
            error={diaryInitDateError}
            value={diaryInitDate}
          />
        </InputView>

        <BottomView>
          <Button
            title={'next'}
            hasBackgroundColor={true}
            onPress={async () => {
              setLoad(true);
              await validateForm();
              setLoad(false);
            }}
          />
          <Button
            title="Cancel"
            onPress={() => {
              navigation.navigate('Home');
            }}
          />
        </BottomView>
      </Container>

我认为我使用了一些使 KeyboardAvoindView 无效的属性。我不想使用 ScrollView,它可能解决这个问题吗?有谁可以帮忙?

【问题讨论】:

    标签: react-native layout keyboard


    【解决方案1】:

    我也一直在努力让KeyboardAvoidingView 正常工作,对我有用的是将这些道具用于KeyboardAvoidingView 并将内容包装在ScrollView 中:

        <KeyboardAvoidingView enabled keyboardVerticalOffset={90} behavior={ Platform.OS === 'ios' ? 'padding' : false } style={{ flex: 1 }} >
            <ScrollView>
    
                {all content here}
           
             </ScrollView>
        </KeyboardAvoidingView>
    

    我还必须使用来自react-nativeTextInput,它需要有scrollEnabled={false} 属性

    【讨论】:

    • 是的,使用 scrollView 我可以做到。但是,我认为在不使用滚动的情况下有任何解决方案。我的布局只有 2 个字段。我看到了一些没有滚动工作的例子,但我不能在这里工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    相关资源
    最近更新 更多