tags:

views:

51

answers:

1

On iPhone, when adding a subview to the window view manually, do I need to account myself for the 20px of the top status bar? That is, do I need to create views with a frame set to (0, 20, 320, 460):

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
[window addSubview:myView];

If so, are there some official guidelines related to this? I just got stuck on it, and after some experimenting it seems that it is indeed the case. This would imply that when a view is created from a nib file, it's always silently shifted by 20 pixels down because when one uses the Interface Builder, the view height of already set to 460px and when the view is added to window, it's already properly positioned.

On the other hand, this looks a bit clunky and shortsighted. Why would Apple hardcode the 20 pixels and do the magic offsetting? Why would I, as a programmer, need to know and assume anything about the status bar? Why is not the window already offset by itself? Is it a known limitation / issue? Or does it have a reason?

A: 

When you are setting view frames, not usually.

The status bar effectively shrinks your canvas.

Now, I HAVE seen some oddball exceptions but none of them are pertinent when doing basic interface work.

Jasconius
Hmm - when I create a view like this: UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)]; myView.backgroundColor = [UIColor redColor]; and add it to the window, the view is completely covered by the status bar. But I agree with the oddballs. I've seen people complaining that in some special cases views may get offset by 20px.
Jan Zich
If you take a look at any UIViewController XIB and simulate the status bar, you can see clearly that the coordinate system is 0,0 just under the status bar... maybe UIWindow is a different case as a parent view.
Jasconius