问题
I have UIWebView loaded with an HTML file. I want to know the position (means x and y cordinates) of an HTML element on the screen when the webview is loaded.
Thanks in advance
回答1:
You may need to a combination of JavaScript and Objective-C to achieve this. You need a JavaScript function to pass back the screen position of certain element, and then translate that position to the screen on the iPhone.
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
One way is to write a JavaScript that takes an element ID and returns its position and the screen resolution. Then translate that to the webview resolution on iPhone screen. It gets trickier if the user is allowed to zoom and pan the webview though.
来源:https://stackoverflow.com/questions/3360139/getting-the-position-of-an-html-element-in-uiwebview