views:

2576

answers:

2

The position of a UIView can obviously be determined by view.center or view.frame etc. but this only returns the position of the UIView in relation to it's immediate superview.

I need to determine the position of the UIView in the entire 320x480 co-ordinate system. For example, if the UIView is in a UITableViewCell it's position within the window could change dramatically irregardless of the superview.

Any ideas if and how this is possible?

Cheers :)

+11  A: 

That's an easy one:

[aView convertPoint:localPosition toView:nil];

... converts a point in local coordinate space to window coordinates. You can use this method to calculate a view's origin in window space like this:

[aView.superview convertPoint:aView.frame.origin toView:nil];
Nikolai Ruhe
A: 

I'm brand new to iPhone programming (and Cocoa) and I'm losing my mind on this. Why doesn't this approach work for me?

Here's my code in a custom view subclassed from UIView:

- (void) printViewDetails: (MyView *) v {

    NSLog(@"   Origin in local coordinates      x:%f  y:%f",  v.frame.origin.x, v.frame.origin.y);

    // what goes here???
    CGPoint p = [v.superview convertPoint:v.frame.origin toView:nil];

    NSLog(@"   Origin in converted coordinates  x:%f  y:%f",  p.x, p.y);
}

- (void) printViewLayout {

    NSLog(@"Base view:");
    [self printViewDetails:self];
    NSLog(@"Subviews");
    for (MyView *v in [self subviews]){

        if ([v isKindOfClass:[MyView class]]) {
            [self printViewDetails:v];
        }
    }

I created a view and add two subviews - all MyView instances. Here's the output:

2010-08-16 19:57:06.820 MyApp[33011:207] Base view:
2010-08-16 19:57:06.822 MyApp[33011:207]    Origin in local coordinates      x:30.000000  y:70.000000
2010-08-16 19:57:06.823 MyApp[33011:207]    Origin in converted coordinates  x:0.000000  y:0.000000
2010-08-16 19:57:06.824 MyApp[33011:207] Subviews
2010-08-16 19:57:06.825 MyApp[33011:207]    Origin in local coordinates      x:0.000000  y:0.000000
2010-08-16 19:57:06.825 MyApp[33011:207]    Origin in converted coordinates  x:0.000000  y:0.000000
2010-08-16 19:57:06.826 MyApp[33011:207]    Origin in local coordinates      x:69.000000  y:0.000000
2010-08-16 19:57:06.827 MyApp[33011:207]    Origin in converted coordinates  x:69.000000  y:0.000000

I've tried a dozen other approaches using convertPoint:toView, fromView, etc. and nothing works.

Any help is appreciated!

Thanks, David

David
Hey David,I'd start a new question for this if I were you.
mtc06