How to make a UITextField move up when keyboard is present? - 我已经尝试了这里提出的所有方法,但我的scrollview没有基于键盘向上移动。我想在iPad笔记中实现移动,因为文本接近键盘,文档向上移动。
答案 0 :(得分:9)
我是这样做的。不要忘记在UITextField上设置委托
- (void) scrollViewAdaptToStartEditingTextField:(UITextField*)textField
{
CGPoint point = CGPointMake(0, textField.frame.origin.y - 1.5 * textField.frame.size.height);
[scrollView setContentOffset:point animated:YES];
}
- (void) scrollVievEditingFinished:(UITextField*)textField
{
CGPoint point = CGPointMake(0, 0);
[scrollView setContentOffset:point animated:YES];
}
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
[self scrollViewAdaptToStartEditingTextField:textField];
return YES;
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[self scrollVievEditingFinished:textField];
return YES;
}
答案 1 :(得分:-1)
这是swift 3.0版本
func scrollViewAdapt(toStartEditing textField: UITextField) {
let point = CGPoint(x: CGFloat(0), y: CGFloat(textField.frame.origin.y - 1.7 * textField.frame.size.height))
scrollView.setContentOffset(point, animated: true)
}
func scrollVievEditingFinished(_ textField: UITextField) {
let point = CGPoint(x: CGFloat(0), y: CGFloat(0))
scrollView.setContentOffset(point, animated: true)
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
self.scrollViewAdapt(toStartEditing: textField)
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
self.scrollVievEditingFinished(textField)
return true
}