UIScrollView幻像子视图 [英] UIScrollView phantom subviews
问题描述
我正在使用以下命令从nib文件加载视图:
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];
nib包含一个名为gridScrollView的滚动视图,在AnalysisView实现文件中,我有一个方法可以将视图添加为滚动视图的子视图:
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++;
}
我已将scrollviews委托设置为AnalysisView并连接了do end decelaring方法
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]);
}
当scrollViewDidEndDecelerating方法触发时,它会报告子视图的数量为5。这些子视图是我期望的GridViews。但是有2个UIImageViews,我不知道为什么它们在那里。
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.
这是一个问题,因为我打算在scrollViewDidEndDecelerating方法中使用viewWithTag检索视图并调用一个视图上的方法,但每当我尝试检索标记为0的视图时,我将检索其中一个UIImageView,这会导致我的应用程序崩溃,因为无法在图像视图上调用该方法。
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.
我知道一种方法是将我的GridViews存储在一个单独的实例数组中并从那里引用它们。但我很想知道2个UIImageView是什么以及它们是如何到达那里的。
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
默认包含2 UIImageViews
作为滚动指示符的子视图。虽然我在文档中找不到关于滚动指示器实现的具体内容,但这些imageview存在于类声明中(参见 UIScrollView.h
标题):
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;
您也可以开始分配不是0的标签,而是从某个正数开始分配 - 这样可以避免与标准子视图
You can also start assigning tags not from 0, but from some positive number - that way avoiding collision with standard subviews
这篇关于UIScrollView幻像子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!