UIScrollView phantom subviews(UIScrollView 幻象子视图)
问题描述
我正在使用 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++;
}
我已将滚动视图委托设置为 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.这些子视图中的 3 个是我期望的 GridView.但是有 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导致我的应用程序崩溃,因为无法在图像视图上调用该方法.
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.
我知道一种解决方法是将我的 GridView 存储在一个单独的实例数组中并从那里引用它们.但我很想知道这 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
作为滚动指示器的子视图.虽然我在文档中找不到任何关于滚动指示器实现的具体信息,但这些图像视图存在于类声明中(参见 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 幻象子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIScrollView 幻象子视图
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01