【问题标题】:KeyboardAvoidingView keyboardVerticalOffset error in react-nativereact-native 中的 KeyboardAvoidingView keyboardVerticalOffset 错误
【发布时间】:2021-06-01 17:45:16
【问题描述】:

我使用 keyboardVerticalOffset 在 react-native 的 KeyboardAvoidingView 中使用静态按钮。 但是

红色箭头表示意外行为。 如您所见,正在添加额外的空间,但它是一个白色区域。

这是我的代码。

const Code = () => {
  const emailInput = useRef(null);
  const birthInput = useRef(null)
  const scrollViewRef = useRef(null);

  const [email, setEmail] = useState<string>('');


  const scrollToEmail = () => {
// I want to scroll the y position of the scroll to the center where the TextInput's cursor is focused.
  };

  return (
    <SafeAreaView style={{ flex: 1}}>
      <KeyboardAvoidingView style={{flex: 1}} behavior={'height'} keyboardVerticalOffset={73 + 50}>
        <ScrollView keyboardShouldPersistTaps="always" ref={scrollViewRef}>
          
          <InputBox> <== height: 50
            <TextInput
              ref={emailInput}
              onFocus = {() => scrollToEmail()} 
          />
          </InputBox>
        
          <InputBox> <== height: 50
            <TextInput
              ref={birthInput}
              onFocus = {() => scrollToBirth()}
            />
          </InputBox>
        </ScrollView>
      </KeyboardAvoidingView>
      
      <View style={{position: 'absolute', bottom: 0, width: '100%'}}>
        <BottomBtn check={check} onPress={submit} text={`ok`} /> <== height: 73
      </View>

    </SafeAreaView>
  );
};

export default Code;

&lt;KeyboardAvoidingView behavior={'padding'} keyboardVerticalOffset={73}/&gt; 不工作。键盘立即关闭。

我希望 Sticky 按钮和 TextInput 的空间完美匹配。我该怎么办?

【问题讨论】:

    标签: react-native react-native-android scrollview textinput react-functional-component


    【解决方案1】:

    我之前也在滚动视图上使用了带有高度的KeyboardAvoidingView,但得到了相同的结果。我的建议是尝试不同的behaviour,但我认为使用react-native-keyboard-aware-scroll-view 更好。它具有更多的属性,并且更易于使用。

    【讨论】:

    • 但我需要 scrollView 中的“ref”。 KeyboardAwareScrollView 很好用但是用起来很麻烦 ref
    • 我不想找到 KeyboardAvoidingView 的替代品。我想要一个解决方案
    猜你喜欢
    • 2019-10-13
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 2022-01-10
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多