我在我的一个VC中有一个uiscrollview。在scrollView中,我有多个TF,按钮等。根据Apple文档,我使用下面的代码在调用键盘通知时向上移动scrollView,以推高隐藏的文本字段。但是,当我关闭键盘时,scrollView不会重置或向下移动,它只是停留在"向上移动"位置。
我在这里遗漏了什么吗?我有一个名为的成员变量:
var activeTextField: UITextField!
我是否正确使用了委托方法?
(func registerForKeyboardNotifications() {
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self,
selector: "keyboardWillBeShown:",
name: UIKeyboardWillShowNotification,
object: nil)
notificationCenter.addObserver(self,
selector: "keyboardWillBeHidden:",
name: UIKeyboardWillHideNotification,
object: nil)
}
func keyboardWillBeShown(sender: NSNotification) {
let info: NSDictionary = sender.userInfo!
let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue
let keyboardSize: CGSize = value.CGRectValue().size
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
var aRect: CGRect = self.view.frame
aRect.size.height -= keyboardSize.height
let activeTextFieldRect: CGRect? = activeTextField?.frame
let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
scrollView.scrollRectToVisible(activeTextFieldRect!, animated:true)
}
}
// Called when the UIKeyboardWillHideNotification is sent
func keyboardWillBeHidden(sender: NSNotification) {
let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
func textFieldDidBeginEditing(textField: UITextField!) {
activeTextField = textField
scrollView.scrollEnabled = true
}
func textFieldDidEndEditing(textField: UITextField!) {
activeTextField = nil
scrollView.scrollEnabled = false
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.registerForKeyboardNotifications()
}
override func viewDidDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
答案 0 :(得分:2)
您必须更改 keyboardWillBeHidden
而不是:
let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
写:
let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, 0, 0)
我希望能帮到你
答案 1 :(得分:2)
我这样做了,它对我有用。似乎比你的方式容易多了。我为我的项目使用了UITextView。但它应该与UITextField一样。 - / + 25只是为了让标题显示出来。
func textViewDidBeginEditing(textView: UITextView) {
myScrollView.setContentOffset(CGPointMake(0, textView.frame.origin.y-25), animated: true)
}
func textViewDidEndEditing(textView: UITextView) {
myScrollView.setContentOffset(CGPointMake(0, -textView.frame.origin.y+25), animated: true)
}
答案 2 :(得分:0)
除了Anna的答案,你也应该使用
let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0.0, keyboardSize.height, 0.0)
中的keyboardWillBeShown
,以避免文本视图在导航栏下方消失。
答案 3 :(得分:-1)
问题在于
var activeTextField: UITextField!
您应该将activeTextField替换为其他UITextField(例如,故事板中存在的文本字段),您可以发现当键盘出现时,滚动视图可以滚动。