Andrew Lauer Barinov
Andrew Lauer Barinov

Reputation: 5754

Centering a UIImageView inside a UIScrollView

I am trying to center a UIImageView in a scroll view. Here is the code I tried with no results.

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kReceiptDetailScrollViewBackgroundCornered]];
[iv setContentMode:UIViewContentModeCenter];
[[self scrollView] insertSubview:iv atIndex:0];

Anybody know what's wrong with this?

Upvotes: 0

Views: 150

Answers (1)

Marcin Kuptel
Marcin Kuptel

Reputation: 2664

You set the image view's content mode to center, which means that the image is not scaled and it can be larger than the image view that displays it.

UIImage *image = [UIImage imageNamed: kReceiptDetailScrollViewBackgroundCornered];
UIImageView *iv = [[UIImageView alloc] initWithImage: image];
iv.contentMode = UIViewContentModeScaleAspectFit;
iv.frame = [self scrollView].bounds;
[[self scrollView] addSubview: iv];

Upvotes: 1

Related Questions