Vojto
Vojto

Reputation: 6949

Changed image of UIImageView from code, not centered anymore

So I just spent like one hour Googling for some answers.

  1. I added UIImageView with Interface Builder and I set image to some existing image.
  2. I wanted to make the image centered, so I changed mode to centered. Worked.
  3. Now I wanted to change the image from code. So I set image in IB to nothing and I did imageView.image = (UIImage instance)

Now the image changed, but it's not centered anymore.

Also, here's an interesting thing: I tried also not changing image in IB, I just left it set to some image and then I changed image from code. I could see both images! The one set in IB was centered (like I want it) and the new one was overlaying on the left.

Any ideas please?

Also I probably lack some basic knowledge of this View stuff, can anyone please point me at documentation where this is explained?

Upvotes: 2

Views: 505

Answers (2)

Vojto
Vojto

Reputation: 6949

I got it! Thanks James Huddleston for useful comments.

You were right. There must have been to UIImageViews.

Here's what happened. I actually used viewWithTag: to find that view (it wasn't linked directly - it was in a UITableViewCell). Well, it apparently didn't work with 0, when I changed tag to 1 - it worked.

Upvotes: 0

James Huddleston
James Huddleston

Reputation: 8448

It sounds like you have two overlapping UIImageView subviews. One of them is referenced by imageView in your view controller and doesn't center. The other isn't referenced by your view controller but does center. The solution would be to get rid of the non-referenced image view.

Upvotes: 1

Related Questions