Thursday, November 11, 2010

How to Center UIImage On-Screen

Created dynamically an UIImageView with small UIImage at top left corner. Wanted it centered, but UIImage automagically resized UIImageView, which was by default stuck at that forementioned top left corner.
UIImage *image = [UIImage imageNamed:@"small.png"];
self.imageView = [[UIImageView alloc] initWithImage:image];
There are several ways to fix this, some more complicated while some more difficult. Common thing is that they contain a lot of code to infest with bugs. However simple problems require simple solutions:
self.imageView.center = self.view.center;
[self.view addSubview:self.imageView];
I'm too embarrassed to tell how much time I "invested" until I got to this solution. Let's just say that I learned a lot. Btw this code does not rotate. There are several ways to fix this...

No comments:

Post a Comment