I have a UIView which I create with initWithFrame and it displays fine, exactly where I want.
To test some touch interactivity, I did the following:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"center: %f",self.center.x);
}
When I press the view (and other views like this one), they all correctly log a number that makes sense as their center point in the coordinate space of the views’ superview.
However, when I do the same log statement inside the initWithFrame method, self.center.x does not report a coordinate in the view’s superview’s space. Instead, it reports the point that would be the bounds center, i.e. if the view is 100px wide, it reports 50, for all views.
What I don’t understand is that these views are not moving and the frame is clearly set at the beginning of initWithFrame since it is passed a frame CGRect and the view displays in the right place. So why doesn’t it report its center properly only in that method?
During
-initWithFrame:, your view has not yet been added to a superview.(Some other code later on adds it to a superview — either as part of the nib loading code, or manually.)