0

I want to get the position of a UIButton inside a UIScrollView the problem is that i get the same position for when the UIButton is in it’s original position or if I scroll down…

I’m using NSLog to print the position and I get the same output

  • Without scroll down i get...

    2014-11-05 19:28:02.564 ... {{260, 163}, {370, 225}}
    
  • Even if I scroll down I get the same output...

    2014-11-05 19:28:06.052 ... {{260, 163}, {370, 225}}
    

This is the code i'm using to obtain the position of the UIButton inside the UIScrollView...

NSLog(@"%@", NSStringFromCGRect(CGRectMake(myButton.frame.origin.x, myButton.frame.origin.y, myScrollDown.frame.size.width, myScrollDown.frame.size.height)));

There is a way to get the actual position of the UIButton even after I scrolled down?

There is another way to get the position of the UIButton inside the ScrollView?

I’ve already visited the following links and I have not resolved it yet.

get UIButton inside an UIScrollView absolute screen location

iPhone - Get Position of UIView within entire UIWindow

Cocoa: What's the difference between the frame and the bounds?

Thanks for any help!

Community
  • 1
  • 1
xzrudy
  • 93
  • 1
  • 8

3 Answers3

5

Maybe you can try this

NSLog(NSStringFromCGRect([self.view convertRect:myButton.frame fromView:myButton.superview]));
Leo
  • 24,596
  • 11
  • 71
  • 92
  • Thanks! that's exactly what I need it, with this i figure it out and now i can get the position, no matter if i scroll down or not, thanks!! – xzrudy Nov 06 '14 at 14:27
0

You're getting the position within the scrollview. Probably what you want is the position within the scrollview's superview (perhaps self.view, perhaps something else).

RegularExpression
  • 3,531
  • 2
  • 25
  • 36
0

Your scroll view is going to have a contentOffset value. The frame of the subviews inside of the scroll view don't change when it scrolls. Instead, this contentOffset value is changed. I'm sure there are better ways to do this but on a basic level, you could just do something like so to get the frame in the scroll view's superview:

CGRect frame = myButton.frame;
frame.origin.x -= scrollView.contentOffset.x;
frame.origin.y -= scrollView.contentOffset.y;
NSLog(@"%@", NSStringFromCGRect(frame);
Aaron Wojnowski
  • 6,352
  • 5
  • 29
  • 46
  • I used the code you posted and I tried to print the __contentOffset__ before and after scroll down and i got the same in both cases... I used __NSLog(@"contentOffset: (%f, %f)", MyScrollView.contentOffset.x, MyScrollView.contentOffset.y);__ in both cases and in both i got __contentOffset: (0.000000, 0.000000)__ What am I doing wrong? – xzrudy Nov 06 '14 at 01:58