iOS
iOS

Reputation: 3626

UIScrollview ContentSize magic

I am zooming an UIImageView in a UIScrollView on double tap. Works fine when it is set up in Interface Builder. The magic that I see here is, the contentSize of the scrollView increses/decreases by itself as I zoom in/out. I check for the contentSize in viewDidLoad, its not zero.

I tried the same by removiing the scrollView and imageView from IB and created them programmatically. After adding the imageView and tapGestureRecognizer, I fail to zoom here. When I checked for the reason, the contentSize remains zero everywhere.

When I add them in IB/xib, I am doing nothing with the contentSize. I am not setting it anywhere. I find its woking fine, the contentSize adjusts automatically where I need. But, its is not replicating when I create the scrollView programmatically.

How Can I make it work? I welcome your suggestions.

Here is my code for the reference.

ScrollView created in IB

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    [doubleTap setNumberOfTapsRequired:2];
    [imageView addGestureRecognizer:doubleTap];
    imageScrollView.minimumZoomScale=1.01;
    imageScrollView.maximumZoomScale=5;
    imageScrollView.zoomScale=1.01;
    NSLog(@"height = %f width = %f", imageScrollView.contentSize.height, imageScrollView.contentSize.width);
}

NSLog says

height = 449.479767 width = 320.000000

ScrollView created programmatically

- (void)viewDidLoad {
    [super viewDidLoad];

    imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    [doubleTap setNumberOfTapsRequired:2];
    [imageView addGestureRecognizer:doubleTap];
    imageScrollView.minimumZoomScale=1.01;
    imageScrollView.maximumZoomScale=5;
    imageScrollView.zoomScale=1.01;

    [self.view addSubview:imageScrollView];
    [self.imageScrollView addSubview:imageView];
    NSLog(@"height = %f width = %f", imageScrollView.contentSize.height, imageScrollView.contentSize.width);
}

NSLog says

height = 0.000000 width = 0.000000

Upvotes: 0

Views: 769

Answers (3)

Nimisha Patel
Nimisha Patel

Reputation: 406

The similar answer is given by @Pheel about the zooming of image inside a scrollview using coding.

The link of the answer is given below:

IOS: add imageview in a scrollview to have zoom

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 386018

Is auto layout enabled in your xib? If so, IB is setting up constraints betwixt the image view and the scroll view, and at runtime auto layout uses the constraints to set the scroll view's content size. When you create the views in code, you're not adding enough constraints and not setting the content size directly, so the content size remains zero.

Read Tech note TN2154 for more information.

Upvotes: 0

foundry
foundry

Reputation: 31745

These properties

self.minimumZoomScale=1.01;
self.maximumZoomScale=5;
self.zoomScale=1.01;

should be properties of your scrollView:

imageScrollView.minimumZoomScale=1.01;
imageScrollView.maximumZoomScale=5;
imageScrollView.zoomScale=1.01;

Then additionally you need to set the contentSize property (when using IB you don't need to set it as it is loaded from the xib file):

imageScrollView.contentSize = CGSizeMake(width,height);

using appropriate values.

Additionally you need to set the delegate:

imageScrollView.delegate = self;

And at least one delegate method indicating which subview to zoom:

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
   return scrollView.subviews[0];
}

(actually you are better off setting a tag on the subview or assigning it to a property to get a reference to it, as scrollViews also have built-in subviews)

Upvotes: 1

Related Questions