Search code examples
iosobjective-cipadpaginationuiscrollview

UIScrollView shows all of its content even if contentOffSet and bounds does not cover them in Objective-C


First of all, the weirdness of the problem is that I do not encounter same problem in iPhone, it just happens in iPad. The code below just takes array of Strings which are paths for images in application bundle, and puts these images as imageView to my custom UIScrollView. It's job is just to show a picture for a page. When the user turns the page, it shows the next photo in scrollview's bounds. So the idea is basically paging. It works great for iPhone under the bounds of scrollView. However, in iPad, it works as well actually in its bounds but the problem is that the other images of pages are also shown in screen even if scrollView's bounds and contentOffSet value are not supposed to show them, the pages takes off all the screen as I turned the page even if my masterViewController is also on the screen, so images of scrollView goes beyond of everything on screen and make themselves visible. Actually the images of scrollView are not functional because they do not recognise gesture events, but the one that scrollView currently shows in its bounds. So what could be the problem? I googled it but I think no one has ever encountered the similar problem. The code that I created imageViews for scrollView is below but as I said it works in iPhone by not overflowing other images. Thank you.

- (void)loadImagesAtPaths:(NSArray *)paths inFrame:(CGRect)frame
{
    CGFloat newWidth = CGRectGetWidth(frame)*paths.count; //width for contentSize with respect to number of images so that it covers all images in its contentSize

    for( UIView *view in self.subviews) [view removeFromSuperview]; //removing old imageViews

    self.contentSize = CGSizeMake(newWidth, CGRectGetHeight(frame));

    for( NSUInteger currentImageIndex = 0; currentImageIndex < paths.count; currentImageIndex++){

        UIImage *imageForImageView = [UIImage imageNamed:[paths objectAtIndex:currentImageIndex]];

        CGRect frameForImageView = CGRectMake(CGRectGetWidth(frame)*currentImageIndex, 0, CGRectGetWidth(frame), CGRectGetHeight(frame)); //paging the imageView

        UIImageView *currentImageView = [[UIImageView alloc] initWithFrame:frameForImageView];
        currentImageView.contentMode = UIViewContentModeScaleToFill;
        currentImageView.image = imageForImageView;

        [self addSubview:currentImageView];
    }

    self.pagingEnabled                  = YES;
    self.userInteractionEnabled         = YES;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator   = NO;
    self.scrollsToTop                   = NO;
}

The problem occurs when the orientation is rotated from portrait orientation to landscape orientation. If the app starts in landscape orientation scrollView works as I desire but when I, firstly, rotated it to portrait orientation and then, again, to landscape orientation the problem that I am talking about occurs as you can see in screenshots.

Initially landscape-No problem, when turning the page again no problem. Page that goes off is not shown anywhere on any other view IMAGE App launched in landscape orientation initially - No scrolling IMAGE During scrolling, again no problem since app launched in landscape orientation

When the device is rotated from portrait to landscape orientation and picture scrolled toward left of the screen, everything goes weird the view of masterViewController even goes on detailViewController. How can it happen ? IMAGE View is scrolled left


Solution

  • If I understood correctly you say that other images are overlapping your current image.If that is happening you could bring the scrolled image to top [self bringSubviewToFront:imageview] and then if this fits the screen then it will be the only one visible.

    If you mean anything else , please be more detailed.