2

我正在使用 React Native 的ScrollViewFlatList. 打开键盘时,我希望看到与打开键盘之前相同的屏幕。

我以为我可以scrollTo根据键盘状态使用该方法它无法正常工作。

有没有类似案例的典型实现?

keyboardWillShow(e) {
  const { scrollView } = this;
  const { scrollPostiion } = this.state;
  const { height } = e.endCoordinates;
  this.setState({
    keyboardHeight: height,
    scrollPostiion: scrollPostiion + height,
  });
  scrollView.scrollTo({ x: 0, y: scrollPostiion + height, animated: false });
}

keyboardWillHide() {
  const { scrollView } = this;
  const { scrollPostiion, keyboardHeight } = this.state;
  this.setState({
    keyboardHeight: 0,
    scrollPostiion: scrollPostiion - keyboardHeight,
  });
  scrollView.scrollTo({ x: 0, y: scrollPostiion - keyboardHeight, animated: false });
}

changeNowScrollPosition = (event) => {
  this.setState({
    scrollPostiion: event.nativeEvent.contentOffset.y,
  });
}

              <ScrollView
                ref={(c) => { this.scrollView = c; }}
                keyboardShouldPersistTaps="handled"
                pinchGestureEnabled={false}
                keyboardDismissMode="interactive"
                onScroll={(event) => {
                  changeNowScrollPosition(event);
                }}
                onScrollEndDrag={(event) => {
                  changeNowScrollPosition(event);
                }}
                scrollEventThrottle={16}
              >
4

1 回答 1

-1

使用KeyboardAvoidingView:下面是一个简单的例子:

import React, { Component } from 'react';
import { Text, Button, StatusBar, TextInput, KeyboardAvoidingView, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  state = {
    email: '',
  };

  render() {
    return (
      <View style={styles.container}>
        <StatusBar barStyle="light-content" />
        <KeyboardAvoidingView behavior="padding" style={styles.form}>
          <TextInput
            style={styles.input}
            value={this.state.email}
            onChangeText={email => this.setState({email})}
            ref={ref => {this._emailInput = ref}}
            placeholder="email@example.com"
            autoCapitalize="none"
            autoCorrect={false}
            keyboardType="email-address"
            returnKeyType="send"
            onSubmitEditing={this._submit}
            blurOnSubmit={true}
          />
          <View>
            <Button title="Sign Up" onPress={this._submit} />
            <Text style={styles.legal}>
              Some important legal fine print here
            </Text>
          </View>
        </KeyboardAvoidingView>
      </View>
    );
  }

  _submit = () => {
    alert(`Confirmation email has been sent to ${this.state.email}`);
  };
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    backgroundColor: 'white',
  },
  input: {
    margin: 20,
    marginBottom: 0,
    height: 34,
    paddingHorizontal: 10,
    borderRadius: 4,
    borderColor: '#000000',
    borderWidth: 1,
    fontSize: 16,
  },
  legal: {
    margin: 10,
    color: '#333',
    fontSize: 12,
    textAlign: 'center',
  },
  form: {
    flex: 1,
    justifyContent: 'space-between',
  },
});

请注意:样式很重要。

你可以走了!

于 2019-06-03T05:47:27.483 回答