16

with react native.

I can get the position and size with onLayout, but I want to get the absolute position of a view relative to the screen

please! the absolute position of a view relative to the screen

not the absolute position of a view relative to the parent view

What should I do?

weijia.wang
  • 2,088
  • 2
  • 18
  • 32
  • Possible duplicate of [Get size of a View in React Native](https://stackoverflow.com/questions/30203154/get-size-of-a-view-in-react-native) – Balasubramanian Sep 14 '17 at 10:17

2 Answers2

35

you can use measure(callback) it Determines the location on screen, width, and height of the given view and returns the values via an async callback.

a better example can be found here React Native: Getting the position of an element

<View
  ref={(ref) => { this.marker = ref }}
  onLayout={({nativeEvent}) => {
    if (this.marker) {
      this.marker.measure((x, y, width, height, pageX, pageY) => {
                console.log(x, y, width, height, pageX, pageY);
       })
    }
  }}
 >
Mohamed Khalil
  • 3,036
  • 1
  • 20
  • 26
-3

You might want to use the "onTouchStart". it Determines the location on screen from pageX, pageY of the given view and returns the values.

<View
  onTouchStart={(e) => {console.log('touchMove', e.nativeEvent.pageY)}}
 >

a better example can be found here: https://facebook.github.io/react-native/docs/panresponder.html

Amardeep puvar
  • 155
  • 1
  • 4