//选择UItextfield时自动滚动的代码。
func textFieldDidBeginEditing(textField: UITextField) {
self.scrollView.contentOffset = CGPointMake(0, textField.frame.origin.y - 10);
}
func textFieldDidEndEditing(textField: UITextField) {
self.scrollView.contentOffset = CGPointMake(0, textField.frame.origin.y - textField.frame.origin.y);
}
答案 0 :(得分:0)
您可以使用textField
委托方法textFieldShouldReturn
并考虑以下示例:
func textFieldShouldReturn(textField: UITextField) -> Bool {
//set setContentOffset here
self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
self.view.endEditing(true)
return true
}
希望这会有所帮助。
答案 1 :(得分:0)
在按下文本字段时滚动视图(键盘已启动),您必须在键盘状态下添加NSNotificationCenter
观察者。
只需在viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
然后添加函数以相应地更改self.view.frame.origin.y
。
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y -= keyboardSize.height
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.view.frame.origin.y += keyboardSize.height
}
}