UIScrollView 幻象子视图

时间:2022-11-15
本文介绍了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 底部的 UIButtons 不起作用 下一篇:如何以编程方式在 UIScrollView 中使用 AutoLayout 设置子视图?

相关文章

最新文章