通过键盘向上滚动后,TextView不会移回原始位置

时间:2015-12-18 05:15:32

标签: ios swift uitextview

编辑:添加viewDidLoad代码

所以我做了一些研究,以学习如何移动我的一个文本视图,以便当键盘弹出时,它向上滚动视图,以便键盘不覆盖textview。我似乎能够完成这项工作,但现在我的问题是在解除键盘之后,视图保持原样。它没有' snap'回到原来它在键盘弹出和滚动文本视图之前处于适当的位置。我在代码中遗漏了什么吗?或者我需要添加一些东西吗?

override func viewDidLoad() {
    super.viewDidLoad()
    self.questionInput.delegate = self
    self.answerInput.delegate = self
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target:self, action: "DismissKeyboard")
    view.addGestureRecognizer(tap)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}


var activeView: UITextView?


func textViewDidEndEditing(textView: UITextView) {

    self.activeView = nil
}

func textViewDidBeginEditing(textView: UITextView) {

    self.activeView = textView
}

func keyboardDidShow(notification: NSNotification) {
    if let activeView = self.answerInput, keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        let contentInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: keyboardSize.height, right: 0.0)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
        var aRect = self.view.frame
        aRect.size.height -= keyboardSize.size.height
        if (!CGRectContainsPoint(aRect, activeView.frame.origin)) {
            self.scrollView.scrollRectToVisible(activeView.frame, animated: true)
        }
    }
}

func keyboardWillBeHidden(notification: NSNotification) {
    let contentInsets = UIEdgeInsetsZero
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
}

我的第二个问题是我读过关于需要取消注册键盘通知这是确切的引用..

"当您从视图控制器转换时,不要忘记从这些事件中取消注册。"

这究竟是什么意思?在segueing时是否需要添加代码?

任何帮助都表示赞赏,因为我对这两个问题都非常感兴趣。谢谢。

2 个答案:

答案 0 :(得分:2)

如果您处于开发的初始阶段,您可以使用此库来处理整个项目中的键盘处理

https://github.com/hackiftekhar/IQKeyboardManager

关于您的第二个问题,这里是您需要实施的示例代码

override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}

如果您使用autolayout,则需要实现此

func keyboardWasShown(notification: NSNotification) {
    var info = notification.userInfo!
    var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.bottomConstraint.constant = keyboardFrame.size.height + 20
    })
}

答案 1 :(得分:0)

试试这个

func keyboardWillHide(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
            let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, keyboardSize.height, 0)

            self.scrollView.contentInset = insets
            self.scrollView.scrollIndicatorInsets = insets

        }
    }