我有一个UIScrollView,它包含一个按钮。当按下按钮时,我想使用scrollRectToVisible滚动到视图的底部。
例:
CGRect r = CGRectMake(0, myUIScrollView.contentSize.height - 1, 1, 1);
[myUIScrollView scrollRectToVisible:r animated:YES];
如果我将动画设置为“不”,那么一切都如预期的那样工作,但如果我将其设置为“是”,我就会看到非常奇怪的行为:
我把_geScroll_Settings.contentSize打印出来了,就像预期的那样.
我还试图通过启动一个计时器来延迟对scrollRectToVisible的调用,但是结果几乎是一样的。
scrollView是相当普通的。我在界面构建器中创建它。我正在启动时动态地添加滚动视图的内容,并适当地调整它的contentSize,但这一切似乎都很好。
有什么想法吗?
发布于 2011-08-23 18:07:26
我打赌scrollRectToVisible退出是因为可见区域无效(1x1),或者y偏移量刚好超出界限,您是否尝试过用scrollView的可见区域大小来设置它?
CGRect rectBottom = CGRectZero;
rectBottom.size = myUIScrollView.frame.size;
rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
rectBottom.origin.x = 0;
[myUIScrollView scrollRectToVisible:rectBottom animated:YES];
对不起,我不能帮你更多,但我现在不在我的Mac上,所以我不能运行测试。上面的代码将创建一个CGRect,其大小正好适合scrollView可见部分,偏移量将是其中的最后一个可见部分。
发布于 2013-12-21 05:39:02
我遇到了一个类似的问题,包括“如果我将动画设置为0,一切都按预期工作”部分。
结果是,在iOS 6上,UITextView自动滚动其最近的父UIScrollView,使光标在成为第一个响应器时可见。在iOS 7上没有这样的行为。UIScrollView似乎被同时对scrollRectToVisible的两个调用弄糊涂了。
在iOS 6上,我对scrollRectToVisible的显式调用大部分时间都被忽略。它只会滚动使UITextView的第一行可见(自动滚动),而不是像在iOS 7上所做的那样。
要测试它,在Xcode 5中创建一个新的单一视图应用程序,将其部署目标设置为6.0,并为ViewController.m使用下面的代码。在iOS 6.1模拟器中运行它,滚动使UITextView隐藏并点击屏幕上的任何位置。您可能需要重试几次,但在大多数情况下,它只会使第一行可见。如果您重新启用WORKAROUD,那么UITextView就会嵌入到它自己的UIScrollView中,而对scrollRectToVisible的调用也会像预期的那样工作。
#import "ViewController.h"
//#define WORKAROUND
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap)]];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
self.scrollView.contentSize = CGSizeMake(320, 400);
self.scrollView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.scrollView];
#ifdef WORKAROUND
UIScrollView* dummyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
self.textView = [[UITextView alloc] initWithFrame:dummyScrollView.bounds];
[dummyScrollView addSubview:self.textView];
[self.scrollView addSubview:dummyScrollView];
#else
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
[self.scrollView addSubview:self.textView];
#endif
self.textView.backgroundColor = [UIColor grayColor];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewTap
{
if (self.textView.isFirstResponder) {
[self.textView resignFirstResponder];
}
else {
[self.textView becomeFirstResponder];
}
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
#ifdef WORKAROUND
[self.scrollView scrollRectToVisible:CGRectInset(self.textView.superview.frame, 0, -10) animated:YES];
#else
[self.scrollView scrollRectToVisible:CGRectInset(self.textView.frame, 0, -10) animated:YES];
#endif
}
@end
https://stackoverflow.com/questions/7168780
复制相似问题