繁体   English   中英

如何滚动到 React Native 中的视图?

[英]How to scroll into View in React Native?

我正在使用 React Native 开发一个应用程序。 在那里,我想在按下按钮时滚动到View中。 我试过这样的事情。

render() {
  return(
    <View ref={field => this.myView = field} >
      //something inside the view
    </View>
  )
}

然后,尝试

this.myView.focus()

但是,它不起作用。

我想得到如下 GIF 演示中所示的结果。 这个怎么做?

在此处输入图像描述

您应该存储ScrollView ref 并使用scrollViewRef.scrollTo()

render() {
  return(
    <ScrollView ref={ref => this.scrollViewRef = ref}>
      <View 
        // you can store layout for each of the fields
        onLayout={event => this.layout = event.nativeEvent.layout}
      > 
        // some field goes here
      </View>
    </ScrollView>
  )
}

然后当发生错误时,只需滚动到您的字段this.scrollViewRef.scrollTo({ y: this.layout.y, animated: true }); .

更新:可以在这个 repo中找到一个工作示例。

暂无
暂无

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

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