我以前用过UIScrollView,现在正在使用它,从来没有遇到过问题.我现在将它添加到一个旧的应用程序,虽然它按预期工作(我可以看看内容,用我的手指滚动,所有的边界和大小设置正确,所以内容中没有空白的空间,等等.),我只是无法scrollToRectVisible上班.我甚至简化了调用,以便它只移动到0,0位置:
[scrollView scrollRectToVisible:CGRectMake(0, 0, 10, 10) animated:YES];
Run Code Online (Sandbox Code Playgroud)
或将其移至0,200:
[scrollView scrollRectToVisible:CGRectMake(0, 200, 10, 10) animated:YES];
Run Code Online (Sandbox Code Playgroud)
我甚至做了一个快速的应用来测试这个,我可以scrollRectToVisible像我期望的那样去那里工作.但在我的旧应用程序中,我似乎无法做任何事情.
我可以使用scrollView滚动setContentOffset:,但这不是我想要的.
此scrollView及其内容由IB在nib中定义,并与IBOutlet一起使用.我在我的应用程序中使用的唯一代码来处理scrollView是
[scrollView setContentSize:CGSizeMake(scrollView.contentSize.width, imageView.frame.size.height)];
Run Code Online (Sandbox Code Playgroud)
(我只对垂直滚动而不是水平感兴趣).
有没有人遇到这样的问题?
我比较了两个应用程序中的scrollView属性,它们是相同的.
附录:
我的scrollViews框架是:0.000000 0.000000 480.000000 179.000000
我的scrollViews contentSize是:0.000000 324.000000
它仍然像我想要滚动到的矩形已经可见,并且不需要滚动.不确定这是不是正在发生的事情.这只是最神奇的事情.似乎很容易解决这个问题......
附录#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];
}
Run Code Online (Sandbox Code Playgroud)
关于此scrollView的其他所有内容都按预期工作,但是scrollRectToVisible.为什么?!?任何疯狂的猜测?
我有一个滚动视图,我试图以编程方式滚动到它的底部..
试过这些:
extension UIScrollView {
// Bonus: Scroll to bottom
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
if(bottomOffset.y > 0) {
setContentOffset(bottomOffset, animated: true)
}
}
}
Run Code Online (Sandbox Code Playgroud)
从:
在 Swift 中以编程方式将 UIScrollView 滚动到子 UIView(子视图)的顶部
let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
scrollView.setContentOffset(bottomOffset, animated: true)
Run Code Online (Sandbox Code Playgroud)
从:
但两者都没有做任何事情......
怎么做?