本文介绍了UIScrollView按预期工作,但scrollRectToVisible:什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我使用过 UIScrollView ,现在正在使用它,从来没有遇到过问题。我现在将它添加到一个旧的应用程序,虽然它按预期工作(我可以看看内容,用我的手指滚动,所有的边界和大小设置正确,所以内容中没有空白的空间,等等。),我只是无法获得 scrollToRectVisible 才能工作。我甚至简化了调用,只是它移动到0,0位置:

I have used UIScrollView before, and am using it now, and never had a problem. I'm now adding it to an old app, and while it works as expected (I can look at the contents, scroll around with my finger, all the bounds and sizes are setup right so there is no empty space in the content, etc.), I just can't get scrollToRectVisible to work. I have even simplified the call so that it merely moves to the 0,0 position:

 [scrollView scrollRectToVisible:CGRectMake(0, 0, 10, 10) animated:YES];

或将其移至0,200:

or move it to 0,200:

 [scrollView scrollRectToVisible:CGRectMake(0, 200, 10, 10) animated:YES];

我甚至做了一个快速的应用来测试这个,我可以得到 scrollRectToVisible 按照我的预期在那里工作。但在我的旧应用程序中,我似乎无法做任何事情。

I even made a quick app to test this and I can get scrollRectToVisible to work there as I expect. But in my old app, I can't seem to make it do anything.

我可以使用 setContentOffset创建scrollView滚动:,但这不是我想要的。

I can make the scrollView scroll with setContentOffset:, but that's not what I want.

此scrollView及其内容由IB在nib中定义,并与IBOutlet一起使用。我在我的应用程序中用来处理scrollView的唯一代码是

This scrollView and its contents are defined in the nib by IB and used with an IBOutlet. The only code I am using in my app to handle the scrollView is

 [scrollView setContentSize:CGSizeMake(scrollView.contentSize.width, imageView.frame.size.height)];

(我只对垂直滚动不感兴趣感兴趣)。

(I'm only interested in vertical scrolling not horizontal).

有没有人遇到这样的问题?

Has anyone run into a problem like this?

我比较了两个应用中的scrollView属性,它们是相同的。

I have compared the scrollView attributes in both apps and they are identical.

ADDENDUM:

我的scrollViews框架是:0.000000 0.000000 480.000000 179.000000

My scrollViews frame is: 0.000000 0.000000 480.000000 179.000000

我的scrollViews contentSize是:0.000000 324.000000

My scrollViews contentSize is: 0.000000 324.000000

它仍然像我想滚动到的矩形已经可见并且不需要滚动。不确定这是不是正在发生的事情。这只是最重要的事情。似乎很容易解决...

It still acts like the rect I want to scroll to is already visible and no scrolling is needed. Not sure if that is what is happening. This is just the darnest thing. Seems like such an easy thing to resolve...

ADDENDUM#2:

ADDENDUM #2:

这就是我的样子没有 scrollRectToVisible

CGPoint point = myRect.origin;
if (![clefScrollView pointInside:point withEvent:nil]) {
    point.x = 0;
    if (point.y > clefScrollView.contentSize.height - clefScrollView.bounds.size.height)
        point.y = clefScrollView.contentSize.height - clefScrollView.bounds.size.height;
    [clefScrollView setContentOffset:point animated: YES];
}

此scrollView的其他所有内容均按预期工作,但 scrollRectToVisible 。为什么?!?任何疯狂的猜测?

Everything else about this scrollView works as expected, but scrollRectToVisible. WHY?!? Any wild guesses?

推荐答案

一个多月后,我终于明白了。虽然上面的替代方案运作良好,但它一直困扰着我,我不得不最终解决它。原来这是一个愚蠢的错误。

Over a month later, and I finally figured it out. While the alternative above worked well, it has been bugging me and I had to finally figure it out. Turns out that it was somewhat of a stupid mistake.

这是我的viewDidLoad中的旧代码,我设置了scrollView:

Here's the old code in my viewDidLoad, where I set up the scrollView:

[clefScrollView setContentSize:CGSizeMake(clefScrollView.contentSize.width, clefView.frame.size.height)];

scrollView高度或宽度的值不能为0!我认为这已经过了我,因为我假设ScrollView的大小开始时有一个有效的大小,我错过了一个维度为零的事实!

The value of a scrollView height or width can't be 0! I think this got past me because I was assuming that ScrollView sizes start out with a valid size, and I was missing the fact that one dimension was zero!

这有效:

[clefScrollView setContentSize:CGSizeMake(clefView.frame.size.width, clefView.frame.size.height)];

希望这有助于那里的人。我确实花了太多时间试图调试它。

Hope this helps someone out there. I definitely spent way too much time trying to debug this.

这篇关于UIScrollView按预期工作,但scrollRectToVisible:什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:25