问题描述
我以前用过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.
此滚动视图及其内容由 IB 在 nib 中定义,并与 IBOutlet 一起使用.我在我的应用程序中用来处理滚动视图的唯一代码是
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.
附录:
我的 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...
附录#2:
这就是我在没有 scrollRectToVisible
的情况下的做法:
This is how I am making do without 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 中的旧代码,我在其中设置了滚动视图:
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: 什么也不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!