当UITextField成为第一响应者时,如何使UIScrollView自动滚动 [英] How to make a UIScrollView auto scroll when a UITextField becomes a first responder
问题描述
我见过有关此处的帖子如果子视图 UITextField
成为第一个响应者,则 UIScrollViews
应自动滚动;但是,我无法弄清楚如何让它发挥作用。
I've seen posts around here that suggest that UIScrollViews
should automatically scroll if a subview UITextField
becomes the first responder; however, I can't figure out how to get this to work.
我所拥有的是 UIViewController
,其中包含 UIScrollView
并且在 UIScrollView
内有多个文本字段。
What I have is a UIViewController
that has a UIScrollView
and within the UIScrollView
there are multiple textfields.
我知道如何在必要时手动执行此操作;但是,根据我一直在阅读的内容,似乎可以将其自动滚动。请帮助。
I know how to do this manually if necessary; however, from what I've been reading, it seems possible to have it autoscroll. Help please.
推荐答案
我希望这个例子可以帮助你
你可以通过这段代码滚动到任何一点。 / p>
I hope this example will help you You can scroll to any point by this code.
scrollView.contentOffset = CGPointMake(0,0);
因此,如果你有文本字段,它必须在视图上有一些x,y位置,所以你可以使用
So if you have textfield, it must have some x,y position on view, so you can use
CGPoint point = textfield.frame.origin ;
scrollView.contentOffset = point
这应该可以解决问题,
但如果您不知道何时调用此代码,那么您应该学习 UITextFieldDelegate 方法
But if you don't know when to call this code, so you should learn UITextFieldDelegate methods
在代码中实现此方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// Place Scroll Code here
}
我希望你知道如何使用委托方法。
I hope you know how to use delegate methods.
这篇关于当UITextField成为第一响应者时,如何使UIScrollView自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!