UIScrollView phantom subviews
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.
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
精彩评论