2

我在 UIScrollView 中有一个 UITextView,一旦用户开始编辑它就需要自动滚动。这是因为键盘会覆盖文本视图。

这是代码 -

viewDidLoad:

feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segmentedControl.frame.origin.x + self.segmentedControl.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);

commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, emailField.frame.origin.y + 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;

这是委托方法的实现 -

-(void)textViewDidBeginEditing:(UITextView *)textView{
    CGPoint point = textView.frame.origin;
    [scrollView setContentOffset:point animated:YES];
}

然而,什么也没有发生。

4

3 回答 3

4

你做得很好但是使用feedBackformView而不是 scrollView,同时在方法中设置内容偏移量, textViewDidBeginEditing:看看下面的代码

feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);

commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;

[feedBackformView addSubview:commentsView];
[self.view addSubview:feedBackformView];

在 textview 委托方法中,

-(void)textViewDidBeginEditing:(UITextView *)textView{
  CGPoint point = textView.frame.origin;
  [feedBackformView setContentOffset:point animated:YES];
 }

希望它会帮助你...

于 2013-04-18T04:49:58.543 回答
1

您可以使用 scrollViewscrollRectToVisible:animatedsetContentOffset:animated此处描述的方法: UIScrollView Class Reference

请记住,UITextView 带有自己的滚动视图。所以你可能不需要将它嵌套在另一个滚动视图中。但是,如果您的应用程序那么有趣,您可能需要这样做。

于 2013-04-18T03:22:16.923 回答
1

您的滚动点是否已经在可见点?

-(void)textViewDidBeginEditing:(UITextView *)textView{
    // try this instead
    CGPoint point = CGPointMake(textView.frame.origin.x, 
                                textView.frame.origin.y + textView.frame.size.height);
    [scrollView setContentOffset:point animated:YES];

    // What does this produce?
    NSLog(@"%@ %@", NSStringFromCGPoint(scrollView.contentOffset),
                    NSStringFromCGRect(textView.frame));
}
于 2013-04-18T03:34:46.303 回答