Search code examples
uiviewuiscrollviewuiimageviewuiimagecontentmode

Large UIImage not scaling down into Scroll View's frame


i can't seem to use UIViewContentModeScaleAspectFit property to scale my imageView within my scrollview to fit the scrollview size.

- (void)loadView {
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.moilum.com/images/atomicongallery/setmenu/setmenu1.jpg"]]];
imageView = [[UIImageView alloc]initWithImage:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:applicationFrame];
[scrollView addSubview:imageView];
scrollView.contentSize = image.size;
scrollView.minimumZoomScale = 0.3;
scrollView.maximumZoomScale = 3.0;
scrollView.clipsToBounds = NO;
scrollView.delegate = self;
self.view = scrollView;

}

i have tried imageView.contentMode = UIViewContentModeScaleAspectFill as well. scrollView.contentmode = UIViewContentModeScaleAspectFill as well. All dont seem to work :(

any way out!?

Cheers!


Solution

  • The UIScrollView will not adjust its zoomScale if you don't implement the - (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView method on the UIScrollViewDelegate.

    Something like this

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView 
    {
      NSArray *subViews = [sView subviews];
      if([subViews count] > 0){
         return [subViews objectAtIndex:0];
      }
      return nil;
    }
    

    (But apparently not setting a delegate at all works too.)