3

I've read this iPhone - Get Position of UIView within entire UIWindow and others multiple times, but just cannot get my app to output to me the absolute position of something on the screen.

I have a scrollview that is jumping around and I'm trying figure out why. I'd like to be able to check where it is on screen (not its frame relative to its superview). I've tried the following:

CGPoint point = [scrollView.superview convertPoint:scrollView.frame.origin toView:nil];
NSLog(@"point is %f", point.y);

yet the console shows 0 as the Y position of the scroll view, despite that the fact that the scrollview is not in fact there on my screen, it is about a third of the way down. Does this method not give you the position of something in the absolute, entire 320x568 screen? I'm sure it's something small, but could someone point out what i'm missing?

Community
  • 1
  • 1
skinsfan00atg
  • 571
  • 1
  • 3
  • 19
  • Are you sure it is the scroll view itself, not its contents jumps on the screen? Does your window take up entire screen? – yurish Dec 03 '13 at 18:18
  • thanks for the help! i had thought this previously so i shaded the scrollview in the nib to make sure and it is in fact the scrollview itself moving around. i just figured it'd be easy to capture somethings absolute position on screen, but i must have something else going on preventing convertPoint from working – skinsfan00atg Dec 03 '13 at 18:32
  • At what place do you try to get screen coordinates? Is it in viewDidLoad? – yurish Dec 03 '13 at 18:32
  • i had tried a couple places (not viewdidload), but maybe thats my problem. i can try and find the last method that runs to get the final position of it – skinsfan00atg Dec 03 '13 at 18:35
  • 1
    You can try this in viewDidAppear if you show the scroll view at start up. – yurish Dec 03 '13 at 18:37
  • that did it! i can see the real position on screen now. so to use this, it has to be at at least this point in the setup i guess? i have it in some other methods and its not working. so perhaps ill need to call something from didappear – skinsfan00atg Dec 03 '13 at 18:42
  • Well, vieDidLoad is called when view controller's root view has been attached to the view hierarchy (and this is necessary for successful conversion), I do not know of any other methods that called earlier with the root view attached. – yurish Dec 03 '13 at 18:47

2 Answers2

4

UIScrollView has a property called contentOffset. If you want to know the absolute position, you'll want to take the position of the view (that's in the scroll view) and minus the scroll view's contentOffset.

Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • thanks so much for the response. I'm actually trying to get the position of the scrollview itself, nothing inside of it. do i still take the offset into account if i just want where that scrollview is on screen? – skinsfan00atg Dec 03 '13 at 18:19
  • No, the offset only affects the subviews of the scroll view. – Guy Kogus Dec 04 '13 at 09:31
-1
UIView *parentview =  [[UIApplication sharedApplication] window].rootViewController.view
CGPoint point = [parentview convertPoint:scrollView.frame.origin toView:nil];
NSLog(@"point is %f", point.y);
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
  • thanks so much for the response. I'm getting an error when i try this. it says "property rootviewcontroller not found on object of type NSArray". – skinsfan00atg Dec 03 '13 at 18:18
  • 1
    @AndrewGray use Window instead on Windows. You just need keyWindow. Here are alternatives to get it.http://stackoverflow.com/questions/3782066/how-to-get-my-uiwindow-using-uiapplication – Kunal Balani Dec 03 '13 at 18:20
  • ok yea window gave me an exception, but i tried key window and that ran. unfortunately it still says 0 in the console. I'm not sure whats going on. maybe theres something wrong with my nib, ill have to keep digging – skinsfan00atg Dec 03 '13 at 18:27
  • what is your view hierarchy ? you can check using property subviews of an UIView – Kunal Balani Dec 03 '13 at 18:28
  • the scrollview is a child of a uiview, which is a child of the main view – skinsfan00atg Dec 03 '13 at 18:32
  • The posted code is broken. It does not do anything meaningful. – Nikolai Ruhe Dec 04 '13 at 20:25
  • I'm not sure what you mean with "ans" but here's my critique: You are converting coordinates the wrong way. `parentView` can't convert the coordinates of `scrollView`'s superview because they are in a different coordinate system. The whole point of `convertPoint:toView:` is that the target view interprets the point in its own coordinate system and converts it to a target view. There's just no sense in selecting some arbitrary view and ask it to do the conversion of a point in an unrelated coordinate system. – Nikolai Ruhe Dec 05 '13 at 09:26
  • @NikolaiRuhe OP had not mentioned about the view hierarchy. If you look at my comments that is what I am trying to figure out. The idea was to use converPoint multiple times (recursively) using subviews property. – Kunal Balani Dec 05 '13 at 15:34