freda42
freda42

Reputation: 75

UIImageView completely wrong position in UIView

I'm trying to add an UIImageView to a UIView that I created. However, the position of the image is way too low inside the UIView, even though I set the frames to be equal. The UIView is called photoView, here is what I did:

UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.JPG"]];
imgV.frame = self.photoView.frame;
[self.photoView addSubview:imgV];

You can see the result (and the position of photoView in the storyboard) here: enter image description here

I have no idea how to fix this, adding

imgV.center = self.photoView.center;
imgV.contentMode = UIViewContentModeScaleToFill;

doesn't help either.

Upvotes: 1

Views: 1291

Answers (2)

iDevzilla
iDevzilla

Reputation: 15558

Try this:

imgV.frame = self.photoView.bounds;

instead of this:

imgV.frame = self.photoView.frame;

Upvotes: 1

freda42
freda42

Reputation: 75

Nevermind, I found the answer, but I'll leave this up here in case anyone else has this question.

The mistake was in the difference between frame and bounds. This is where I found my answer.

My solution was this:

[imgV setFrame:CGRectInset(self.photoView.bounds, 0, 0)];

Upvotes: 0

Related Questions