MaxGabriel
MaxGabriel

Reputation: 7705

Proper contentSize of UIScrollView with UIImageView inside it

I'm placing a UIImageView inside of a UIScrollView, basing my code off of the answer in this question. The problem I'm having is that there is a significant amount of white space to the bottom and right, and I can't scroll to some of the image in the top and left. I figure this is due to me incorrectly setting the contentSize of the scrollView. Here's the relevant code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    _imageView.image = _image;
    _imageView.bounds = CGRectMake(0, 0, _imageView.image.size.width,_imageView.image.size.height);
    _scroller.contentSize = _imageView.image.size;
 }

The view controller I'm in has three properties, a UIScrollView (_scroller), a UIImageView (_imageView), and a UIImage (_image).

Upvotes: 0

Views: 1877

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185841

You're setting the UIImageView's bounds property. You want to be setting its frame property instead. Setting the bounds will resize it around its center point (assuming you haven't changed the underlying CALayer's anchorPoint property), which is causing the frame origin to end up negative, which is why you can't see the upper-left.

_imageView.frame = CGRectMake(0, 0, _imageView.image.size.width, _imageView.image.size.height);

Alternate syntax:

_imageView.frame = (CGRect){CGPointZero, _imageView.image.size};

Upvotes: 3

Related Questions