Nos*_*tap 27 iphone objective-c uiscrollview uitextfield ios
我在这里看到的帖子表明,UIScrollViews如果子视图UITextField成为第一个响应者,应自动滚动; 但是,我无法弄清楚如何让它发挥作用.
我已经是一个UIViewController具有UIScrollView与内UIScrollView有多个文本框.
如果有必要,我知道如何手动完成此操作; 但是,从我一直在阅读的内容来看,似乎可以让它自动滚动.请帮忙.
Ade*_*aiz 33
我希望这个例子可以帮助您.您可以通过此代码滚动到任何点.
scrollView.contentOffset = CGPointMake(0,0);
Run Code Online (Sandbox Code Playgroud)
因此,如果你有文本字段,它必须在视图上有一些x,y位置,所以你可以使用
CGPoint point = textfield.frame.origin ;
scrollView.contentOffset = point
Run Code Online (Sandbox Code Playgroud)
这应该是诀窍,
但是如果你不知道何时调用这段代码,那么你应该学习UITextFieldDelegate方法
在代码中实现此方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// Place Scroll Code here
}
Run Code Online (Sandbox Code Playgroud)
我希望你知道如何使用委托方法.
Sup*_*off 11
我知道这个问题已经得到了回答,但我想我会分享我从@Adeel和@Basil回答中使用的代码组合,因为它似乎在iOS 9上完美适合我.
-(void)textFieldDidBeginEditing:(UITextField *)textField {
// Scroll to the text field so that it is
// not hidden by the keyboard during editing.
[scroll setContentOffset:CGPointMake(0, (textField.superview.frame.origin.y + (textField.frame.origin.y))) animated:YES];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
// Remove any content offset from the scroll
// view otherwise the scroll view will look odd.
[scroll setContentOffset:CGPointMake(0, 0) animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
我还使用了动画方法,它可以实现更平滑的过渡.
小智 6
这是@Supertecnoboff的答案的Swift 4更新。这对我来说很棒。
func textFieldDidBeginEditing(_ textField: UITextField) {
scroll.setContentOffset(CGPoint(x: 0, y: (textField.superview?.frame.origin.y)!), animated: true)
}
func textFieldDidEndEditing(_ textField: UITextField) {
scroll.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
Run Code Online (Sandbox Code Playgroud)
确保扩展UITextFieldDelegate并将文本字段的委托设置为self。
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CGRect rect = [textField bounds];
rect = [textField convertRect:rect toView:self.scrollView];
rect.origin.x = 0 ;
rect.origin.y -= 60 ;
rect.size.height = 400;
[self.scrollView scrollRectToVisible:rect animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37762 次 |
| 最近记录: |