UIScrollView包含比以编程方式循环时存在的视图更多的视图 [英] UIScrollView contains more views than exist when looping programmatically

查看:36
本文介绍了UIScrollView包含比以编程方式循环时存在的视图更多的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式调整滚动视图的内容大小。为此,我浏览其中包含的所有视图,并获得视图原点和视图高度的最大值,然后将内容大小设置为该值加上10。由于某些原因,我在空白处有很多空白我页面的底部。我使用 NSLog()找出它认为这么大的对象,并发现它根本不存在。这是我的代码:

I'm resizing my scroll view's content size programmatically. To do this, I look through all of the views contained within it and get the highest value for the view origin plus the view height, then set my content size to that plus 10. For some reason, I have a lot of white space at the bottom of my page. I used an NSLog() to find out what object it thought was this big and found that it is an object that does not even exist. Here is my code:

- (void)resize_scrollview_to_fit {
    CGFloat scrollViewHeight = 0.0f;
    for (UIView *view in scrollView.subviews) {
        if (view.frame.size.height + view.frame.origin.y > scrollViewHeight) {
            scrollViewHeight = view.frame.size.height + view.frame.origin.y + 10;

            NSLog(@"Scroll View Height: %f \nView Type: %@", scrollViewHeight, view.class);
        }
    }

    [scrollView setContentSize:(CGSizeMake(scrollView.frame.size.width, scrollViewHeight))];
}

输出为:

2014-05-16 10:40:56.757 eTicket[5542:907] Scroll View Height: 175.000000 
View Type: UIImageView
2014-05-16 10:40:56.767 eTicket[5542:907] Scroll View Height: 301.000000 
View Type: UILabel
2014-05-16 10:40:56.773 eTicket[5542:907] Scroll View Height: 345.000000 
View Type: UITextField
2014-05-16 10:40:56.780 eTicket[5542:907] Scroll View Height: 399.000000 
View Type: UILabel
2014-05-16 10:40:56.787 eTicket[5542:907] Scroll View Height: 441.000000 
View Type: UILabel
2014-05-16 10:40:56.794 eTicket[5542:907] Scroll View Height: 520.000000 
View Type: UIImageView

此页面上的滚动视图中只有一个UIImageView,即列出的第一项。该第二UIImageView在代码或情节提要中均不存在。上面的代码应该以441的结果结尾。我没有创建UIImageView的代码,情节提要中仅有的其他2个UIImageViews与UIScrollView处于同一级别,并且不在其中。另外,如果看到它们,它们将得到558。那么,这个额外的UIImageView是从哪里来的,我该如何去除它呢?

There is only one UIImageView on this page within the scroll view, which is the first item listed. That second UIImageView does not exist in either code or the storyboard. The above code should end with the result of 441. I have no code creating an UIImageView, and the only other 2 UIImageViews in the storyboard are on the same level as the UIScrollView and not within it. Plus they would result in given 558 if it was seeing them. So where is this extra UIImageView coming from, and how do I get rid of it?

谢谢,
James

Thanks, James

编辑

真的没有其他解决方案吗?我讨厌Sasha的回答是解决此问题的唯一方法。只是将UIImageView自动添加到UIScrollView似乎不合逻辑。如果他的答案是正确的,那么有人可以确认吗?

Are there really no other solutions for this? I'd hate for Sasha's answer to be the only way to resolve this. It just doesn't seem logical that a UIImageView would just be added automatically to a UIScrollView. If his answer is correct though, can someone confirm it?

谢谢,
James

Thanks, James

推荐答案

您应该保持自己的子视图轨迹,不要依赖滚动视图子视图。例如,第二个 UIImageView 可能是滚动指示器-内部视图,当您滚动到跟踪位置时显示。

You should keep your own track of subviews, don't rely on scroll view subviews. For example the second UIImageView might be a scroll indicator - an internal view, displayed when you scroll to track position.

这篇关于UIScrollView包含比以编程方式循环时存在的视图更多的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆