Resolved UIScrollView does not scroll a UIImageView

Discussion in 'iOS Programming' started by Tex-Twil, Nov 17, 2013.

  1. Tex-Twil, Nov 17, 2013
    Last edited: Nov 20, 2013

    Tex-Twil macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe
    #1
    Hi,
    I have a UIScrollView that is setup in the Interface Builder and take the entire screen. I want to add an UIImageView so that the user can scroll horizontally and vertically through the image.

    In my viewDidLoad method I've added this:

    Code:
        self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01.jpeg"]];
    
        [self.view addSubview:self.imageView];
        self.scrollView.contentSize = self.imageView.image.size;
        self.scrollView.scrollEnabled = YES;
        self.scrollView.clipsToBounds = YES;
    
        NSLog(@"self.scrollView.frame %@", NSStringFromCGRect(self.scrollView.frame));
        NSLog(@"self.scrollView.bounds %@", NSStringFromCGRect(self.scrollView.bounds));
        NSLog(@"self.imageView.frame %@", NSStringFromCGRect(self.imageView.frame));
        NSLog(@"self.scrollView.contentSize %@", NSStringFromCGSize(self.scrollView.contentSize));
    
    But the scroll view is not scrolling :( The log output is

    Code:
    self.scrollView.frame {{0, 0}, {320, 568}}
    self.scrollView.bounds {{0, 0}, {320, 568}}
    self.scrollView.contentSize {2048, 1365}
    self.imageView.frame {{0, 0}, {2048, 1365}}
    

    what did I do wrong ?

    cheers
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    You are adding your imageView to the wrong parent view. Try adding it to the scrollView. Also I don't see where you are adding your scrollView to it's parent. Perhaps via a XIB file?
     
  3. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #3
    as xStep said, in this line

    Code:
    [self.view addSubview:self.imageView];
    
    you add the image view to your view controller's view but you should be adding it like

    Code:
    [self.scrollView addSubview:self.imageView];
    
    Note: this is assuming you have connected the UIScrollView as a property named scrollView through the interface builder
     
  4. Tex-Twil thread starter macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe
  5. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #5

Share This Page