18

I am loading a view from a nib file using:

NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"AnalysisView" owner:self options:nil];
AnalysisView *gridView = [nibViews objectAtIndex: 0];

The nib contains a scrollview called gridScrollView and in the AnalysisView implementation file I have a method which adds views as subviews to the scrollview:

for (NSInteger i = [results count] -1; i >= 0; i--) 
{
    Result *result = [results objectAtIndex:i];
    [self loadResult: result];
}

- (void) loadResult: (Result *) result
{
    NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"GridView" owner:self options:nil];
    GridView *gridView = [nibViews objectAtIndex: 0];
    gridView.tag = self.graphCount;

    CGRect gridFrame = gridView.frame;

    CGFloat yOffset = gridFrame.size.height * self.graphCount;
    gridView.frame = CGRectMake(0, yOffset, gridFrame.size.width, gridFrame.size.height);

    [self.gridScrollView addSubview: gridView];
    self.gridScrollView.contentSize = CGSizeMake(self.gridScrollView.frame.size.width, (yOffset + gridFrame.size.height));

    self.graphCount++;
}

I have set the scrollviews delegate to be AnalysisView and hooked up the did end decelaring method

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
      NSLog(@"%d", [scrollView.subviews count]);
}

When the scrollViewDidEndDecelerating method fires it reports that the number of subviews is 5. 3 of these subviews are GridViews which I expect. However there are 2 UIImageViews which I have no idea why they are there.

This is an issue because I intend on retrieving the views with viewWithTag in the scrollViewDidEndDecelerating method and calling a method on the view, however whenever I try to retrieve a view with a tag of 0 I will retrieve one of the UIImageView's and this causes my app to crash because the method cannot be called on an image view.

I know a way round this is to store my GridViews in a seperate instance array and reference them from there. But I'm curious to know what the 2 UIImageViews are and how they got there.

1 Answer 1

45

UIScrollView by default contains 2 UIImageViews as subviews for scroll indicators. Although I can't find anything specific about scroll indicators implementation in docs, those imageviews are present in class declaration (see UIScrollView.h header):

UIKIT_CLASS_AVAILABLE(2_0) @interface UIScrollView : UIView <NSCoding> {
    ...
    UIImageView* _verticalScrollIndicator;
    UIImageView* _horizontalScrollIndicator;

You can also start assigning tags not from 0, but from some positive number - that way avoiding collision with standard subviews

3
  • Thanks - Do you know where I could find any information in the documentation about this?
    – JFoulkes
    Apr 14, 2011 at 13:58
  • I did try just creating a UIScrollView in code and checking the subview count of it but it was zero. I can only guess it doesn't add the subviews until it is drawn on screen
    – JFoulkes
    Apr 14, 2011 at 14:14
  • 6
    probably yes + try playing with showsHorizontal/VerticalScrollIndicator property - if I remember correctly setting it to NO effectively removes indicator from scrollview (but I may be wrong here)
    – Vladimir
    Apr 14, 2011 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.