Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is it possible to get the screen position of an element / component? #1374

Closed
MossP opened this issue May 22, 2015 · 12 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@MossP
Copy link

MossP commented May 22, 2015

use case:
I would like to add a small popUp menu at the location of a button on the page. THe button would be inside a list view though so isn't in a fixed position.

Thanks, and keep up the good work! :) 👍

@JohnyDays
Copy link
Contributor

I believe what you are looking for is

var RCTUIManager = require('NativeModules').UIManager;
//The following code executes inside one of your component's methods, post render
var view = this.refs['ref']; // Where view is a ref obtained through <View ref='ref'/>
var handle = React.findNodeHandle(view); 
RCTUIManager.measure(handle, (x, y, width, height, pageX, pageY) => {
   //Do stuff with the values
})

@MossP
Copy link
Author

MossP commented May 23, 2015

I believe you are right. Thank you :)

@MossP MossP closed this as completed May 23, 2015
@bakso
Copy link

bakso commented May 24, 2015

But how to add the popular menu dynamic?

@JohnyDays
Copy link
Contributor

I'm sorry, I'm afraid I didn't understand the question. You want to add a menu on the position of an element?

@MossP
Copy link
Author

MossP commented May 24, 2015

Hi @iostalk. I am implementing the menu by using @brentvatne's modal plugin then passing a complete collection of stylds into it (take a look at the modalstyle included in the the source).. you can then pass in the screen coordinates (derived from @JohnyDays code) that you'd like the modal window to show based on the size of your modal / touchable object.

@anshul-kai
Copy link

The solution to use UIManager is no longer working on Android with React Native 0.42.0

@wasa4587
Copy link

@a-koka
you can do it this way

              <View
                ref="Marker"
                onLayout={({nativeEvent}) => {
                  this.refs.Marker.measure((x, y, width, height, pageX, pageY) => {
                    console.log(x, y, width, height, pageX, pageY);
                  })
                }}
              >

or this way


              <View
                ref="Marker"
                onLayout={({nativeEvent}) => {
                  var view = this.refs['Marker'];
                  var handle = findNodeHandle(view);
                  UIManager.measure(handle, (x, y, width, height, pageX, pageY) => {
                    console.log(x, y, width, height, pageX, pageY);
                  })
                }}
              >

@anshul-kai
Copy link

onLayout is a good solution but not ideal for use cases where several layout changes occur thus causing several callbacks. My issue got resolved by adding the collapsible={false} prop to the views being measured.

Thanks to hey99xx for his help in #12966

Collapsable property is explained at https://facebook.github.io/react-native/docs/view.html#collapsable

Views that are only used to layout their children or otherwise don't draw anything may be automatically removed from the native hierarchy as an optimization. Set this property to false to disable this optimization and ensure that this View exists in the native view hierarchy.

However, some of UIManager functions such as measure expect to find a native view; and when it can't it returns undefined values instead of throwing an error. Really it would be better to throw an error. React unfortunately is not intelligent to know you would be measuring the view in the first place.

@rajsuvariya
Copy link
Contributor

In my case, I need to measure the x and y coordinate on the click of an element which is inside a scroll view. So it gives me different px and py everytime I click on the element. also, the value differs based on the scrolling

@gitlovenotwar
Copy link

gitlovenotwar commented Aug 31, 2017

@rajsuvariya did you able to get the position of an element even inside a scrollview?

@rajsuvariya
Copy link
Contributor

@gitlovenotwar No I was not able to get the position inside scroll view. But I had only one component inside scrollview so I removed scrollview and now I have only listview which gives me exact position of elemtn

@ghost
Copy link

ghost commented Jan 10, 2018

@JohnyDays how can i get the position of cursor in ScrollView containing TextInput?

@facebook facebook locked as resolved and limited conversation to collaborators May 29, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

8 participants