首页 > 解决方案 > 从 onFocus 事件中检索 TextInput 的屏幕坐标

问题描述

我需要从 onFocus 事件中检索 TextInput 在屏幕上的位置。我还没有找到有关此的文档,我的问题是我无法从焦点触发的事件中找出元素。这里的代码:

_handleOnFocus(event) {        
console(event.nativeEvent.target.layout.y); // where is the y coordinate?
}

回调插入此处:

<TextInput onFocus={              
onFocus={event => {this._handleOnFocus(event);}}} ... />

标签: javascriptreactjsreact-nativereact-native-ios

解决方案


您可以通过event.target.offsetTop但考虑到它会从最近的非静态父级返回偏移量来访问它。如果需要绝对偏移量,请参考这个问题:Finding element's position relative to the document


推荐阅读