views:

364

answers:

3

I'm asking my ViewController for it's view.center property and drawing a new UIView that centers itself around this "center"... I am getting (160, 250) as a response. but when the new UIView draws it's below center... So I'm wondering who's giving me this info and what it relates to? This is clearly where the center of the view is in relation to the WINDOW if you take into account the 20px height of the status bar. which would push the center of the view down 10px. but when drawing myView it appears to draw in relation to the ViewController.view and not the Window so it appears 20px below center...

I would expect the ViewController to give me its center (160, 230) so I could draw in its center...do I need to manually account for the status bar and subtract 20 from height every time? or is there some view space translation i'm overlooking? From ViewController.m:

- (void)setUpMyView {
// Create my view

MyView *aView = [[MyView alloc] init];
self.myView = aView;
[aView release];
myView.center = self.view.center;
NSLog(@"CenterX: %f, Y: %f", self.view.center.x, self.view.center.y);
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
myView.transform = transform;

[self.view addSubview:myView];

}

console: CenterX: 160.000000, Y: 250.000000

A: 

Hi,

Maybe add it to the Subview before you ask its center and size.

MyView *aView = [[MyView alloc] init];
self.myView = aView;
[aView release];
[self.view addSubview:myView]; // add it to the subview first before asking center.
myView.center = self.view.center;
NSLog(@"CenterX: %f, Y: %f", self.view.center.x, self.view.center.y);
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI_2);
myView.transform = transform;
John Ballinger
A: 

I think I know the answer. You View center is correct!

You forgot to add the iPhone status bar is 20px down from the top of the screen and hence included in your view center. Bingo.

John Ballinger
+4  A: 

It's not "lying" actually, it's giving you an answer in a different coordinate system.

When you get or set a view's center, it is calculated relative to its parent's coordinate system. self.view's parent is the application's window, hence its center is (160, 250) as you surmised. But myView's parent is self.view, which has its own local coordinate system. In this case, that coordinate system is 20 pixels lower than that of the window.

What you want is to find the center of self.myView in its own coordinate system. There are two ways you could do it.

1) You could calculate it based on the bounds property, which is a CGRect that specifies the boundaries of a view in its own coordinate system:

myView.center = CGPointMake(self.view.bounds.size.width / 2,
                            self.view.bounds.size.height / 2);

2) Or you could use UIView's convertPoint:fromView: method to convert the coordinate from the window's coordinate system into that of self.view:

// Pass nil as the source view to convert from the window's coordinate system
myView.center = [self.view convertPoint:self.view.center fromView:nil];
George Dean IV
Another possibility is that self.view.superview is nil. I have encountered that myself with a UITableViewController.
Chris R. Donnelly