Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setContentOffset loses precision?

I have an application where I need to initialize the contentOffset of a UIScrollView to a certain value. I notice when I set the contentOffset, it rounds the points up to the nearest integers. I've tried using both setContentOffset and setContentOffset:animated and still get the same result. When I try to manually add the contentOffset onto the frame origin, I don't get the result I want. Anyone know anyway around this?

UIScrollView *myScrollViewTemp = [[UIScrollView alloc] initWithFrame: CGRectMake(CropThePhotoViewControllerScrollViewBorderWidth, CropThePhotoViewControllerScrollViewBorderWidth, self.myScrollViewBorderView.frame.size.width - (CropThePhotoViewControllerScrollViewBorderWidth * 2), self.myScrollViewBorderView.frame.size.height - (CropThePhotoViewControllerScrollViewBorderWidth * 2))];
[self setMyScrollView: myScrollViewTemp];
[[self myScrollView] setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[self myScrollView] setShowsVerticalScrollIndicator: NO];
[[self myScrollView] setShowsHorizontalScrollIndicator: NO];
[[self myScrollView] setBouncesZoom: YES];
[[self myScrollView] setDecelerationRate: UIScrollViewDecelerationRateFast];
[[self myScrollView] setDelegate:self];
[[self myScrollViewBorderView] addSubview: [self myScrollView]];
[myScrollViewTemp release];

UIImageView *myImageViewTemp = [[UIImageView alloc] initWithImage: self.myImage];
[self setMyImageView: myImageViewTemp];
[[self myScrollView] addSubview:[self myImageView]];
[[self myScrollView] setContentSize: [self.myImage size]];
[[self myScrollView] setMinimumZoomScale: self.previousZoomScale];
[[self myScrollView] setMaximumZoomScale: 10];
[[self myScrollView] setZoomScale: self.previousZoomScale animated:NO];
[[self myScrollView] setContentOffset: self.contentOffset animated:NO];
[myImageViewTemp release];

UPDATE: Updated code.

like image 463
Ser Pounce Avatar asked Oct 07 '22 22:10

Ser Pounce


2 Answers

It appears this isn't a bug on my part, but is the intended behavior by Apple whether on purpose or not. In order to work around this, you need to set the contentOffset in the viewWillLayoutSubviews method of the UIViewController. Apparently, you need to set the contentOffset after the whole view frame has been set.

like image 90
Ser Pounce Avatar answered Oct 12 '22 11:10

Ser Pounce


I too struggled with this issue quite a bit, but it turns out that you could just set the bounds on the scroll view, which does the same thing and actually deals ok with CGFloat values.

CGPoint newOffset = CGPointMake(3.8, 0);

We got the new offset here.

[scrollView setContentOffset:newOffset];

After this line the current offset is (4,0).

scrollView.bounds = CGRectMake(newOffset.x, newOffset.y, scrollView.bounds.size.width, scrollView.bounds.size.height);

And this gives you (3.8, 0)

like image 25
Andrew Avatar answered Oct 12 '22 10:10

Andrew