ios - UIScrollView 以编程方式滚动

标签 ios objective-c uiscrollview uitextview uitextviewdelegate

我在 UIScrollView 中有一个 UITextView,它需要在用户开始编辑它时自动滚动。这是因为键盘会覆盖 TextView 。

这是代码-

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;

这是委托(delegate)方法的实现-

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

然而,没有任何反应。

最佳答案

你做的很好,但是使用 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 中的委托(delegate)方法,

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

希望它能帮助你...

关于ios - UIScrollView 以编程方式滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073020/

相关文章:

ios - 使用 Tabbar 管理 PushViewController

ios - 通过 setcontentsize 调用,scrollview 不会一直向下滚动

ios - 如何为 iPhone 4 英寸和 iPhone 3.5 英寸加载单独的 NIB 文件?

iphone - 单击时如何使自定义按钮键像iPhone按钮键一样大

ios - 有没有办法解决 Apple 的 UITextField Emoji 错误?

objective-c - 特定对象的通知

ios - NSDateComponent 给我一天递增 1

ios - NSDictionary 到 NSMutableArray

ios - 当 scrollView 设置 pagingEnabled 为 YES 时,点击 ScrollView 调用 `scrollViewWillBeginDecelerating`

ios - UIScrollView 覆盖 NavigationBar