I have used constant co-ordinates in code in several places in my iPhone app, particularly when I am programmatically creating a view, setting properties of uiview subclasses.
example:
UIImage* img = [UIImage imageNamed:@"background.png"];
UIImageView *imgView = [[[UIImageView alloc] initWithImage: img] autorelease];
imgView.center = CGPointMake(160, 175); // --> Are these constant usages safe?
[self.view addSubview: imgView];
I have tested it on simulator, device both 3G and 4S and it works.
Should I be worried for future device resolutions or any other gotchas?
If so, how should I make this piece of code, more safe? Any coding principles here?
Thanks
Mbh
It’s not a big deal in most cases, but it’s always better to use a reference.
For example, if your image view is at the middle of the screen, you could use
screensize.width/2instead of160, it’s the same in theory, but first, it’s more clear what you are doing, and second, if the screen were to change, you’d be prepared. Another example would be to usenavigationBar.frame.size.heightinstead of44(which is not likely to change anyway).Also, if you reference other views you laid yourself, and you move one, all of them will move with it ;).
In short, if you have a reference you can use, use it, otherwise, don’t worry.