Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sindhu Prabowo Dilaksono
Courses Plus Student 776 PointsUsing Scroll View to zoom at one Image View problem
I have a scroll view and in it i add an image view. I set the colour red to the background of the scroll view. After zooming the image to a size that is bigger than the scroll view, and after setting the content size of the scroll view to the size of the scaled image view in the method "scrollviewdidendzooming", my image in the image view seems not to be centered. When i checked the size of my content view with my image view after the scale, they are indeed the same. But it still shows the red background and part of the image is also not visible. Maybe i didn't place the code to set the image view to be centered in the correct place or maybe i used a wrong set of commands. I need help. Thanks.
''' Objective C
-
(void)viewDidLoad { [super viewDidLoad];
self.scrollView.delegate = self; self.scrollView.zoomScale = 1.0; self.scrollView.minimumZoomScale = 1.0; self.scrollView.maximumZoomScale = 2.0;
//self.photoWithFrame.center = self.scrollView.center;//photoWithFrame is the image view self.scrollView.contentOffset = CGPointZero; self.scrollView.contentInset = UIEdgeInsetsZero;
self.photoWithFrame.contentMode = UIViewContentModeScaleAspectFit; }
-
(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if(scrollView == self.scrollView){ return self.photoWithFrame; }
return nil; }
-
(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
self.scrollView.frame = CGRectMake(44, 91, 235, 235); self.scrollView.contentSize = CGSizeMake(235 * 0.75 * scale, 235 * scale);
self.photoWithFrame.frame = CGRectMake(1, 1, 233 * 0.75 * scale, 233 * scale);
CGSize boundsSize = self.scrollView.bounds.size; CGRect frameToCenter = self.photoWithFrame.frame;
// center horizontally frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
// center vertically frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
self.photoWithFrame.frame = frameToCenter;
}
-
(void)scrollViewDidZoom:(UIScrollView *)scrollView{ self.scrollView.frame = CGRectMake(44, 91, 235, 235);
CGPoint centerOffset = CGPointMake(0, 0); [scrollView setContentOffset: centerOffset animated: NO];
CGSize boundsSize = self.scrollView.bounds.size; CGRect frameToCenter = self.photoWithFrame.frame;
// center horizontally frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
// center vertically frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
self.photoWithFrame.frame = frameToCenter; }
'''
What could have been the problem? Why is the background of the scroll view showing when the image view should have placed itself in the centre of the content of the scroll view? Because of the red background showing, the same exact area of the red showing is equivalent to the area of the missing image view that is not shown.
1 Answer
Sindhu Prabowo Dilaksono
Courses Plus Student 776 PointsAlright! I've found the problem. The problem was that the image view has to fit the width and height ratios of the image inside it.