2
How to make a UITextField move up when keyboard is present? - 我已經嘗試了這裏提出的所有方法,但我的scrollview沒有基於鍵盤向上移動。當文本靠近鍵盤時,我想要像iPad筆記中那樣執行移動,文件上移。如何自動移動UIScrollView以使UITextfield基於鍵盤可見?
How to make a UITextField move up when keyboard is present? - 我已經嘗試了這裏提出的所有方法,但我的scrollview沒有基於鍵盤向上移動。當文本靠近鍵盤時,我想要像iPad筆記中那樣執行移動,文件上移。如何自動移動UIScrollView以使UITextfield基於鍵盤可見?
下面是我如何做到這一點。不要忘了設置代理上的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;
}
這裏是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
}