当键盘弹出时,如何向上移动整个视图?(Swift)

当键盘弹出时,如何向上移动整个视图?(Swift),swift,Swift,底部的灰色框是文本视图。当我点击文本视图时,键盘将从底部弹出。但是,文本视图已被弹出式键盘覆盖 我应该添加哪些功能,以便在键盘弹出时向上移动整个视图 要检测键盘何时出现,您可以收听NSNotificationCenter NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) NS

底部的灰色框是文本视图。当我点击文本视图时,键盘将从底部弹出。但是,文本视图已被弹出式键盘覆盖


我应该添加哪些功能,以便在键盘弹出时向上移动整个视图

要检测键盘何时出现,您可以收听
NSNotificationCenter

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: “keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
这将调用
键盘将显示
键盘将隐藏
。在这里,您可以使用
UITextfield

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            //use keyboardSize.height to determine the height of the keyboard and set the height of your textfield accordingly
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    //pull everything down again
}

正如Milo所说,要自己完成这项工作,您需要注册键盘显示/隐藏通知

然后,您需要编写代码,计算出键盘在屏幕上隐藏了多少,以及相关字段在屏幕上的位置有多高,这样您就知道要改变多少视图

一旦你完成了,你要做什么取决于你是使用自动布局还是使用自动调整大小的遮罩(也称为“Struts and springs”样式的布局)

我写了一篇关于一个项目的开发人员博客文章,其中包括移动键盘的工作代码。请参阅此链接:


在那篇文章中,寻找底部标题为“滑动视图为键盘腾出空间”的链接。

Swift 4

这段代码并不完美,但值得一试

首先将整个视图嵌入到滚动视图中

将这个可爱的小函数添加到您的类中:

@objc func keyboardNotification(_ notification: Notification) {
    if let userInfo = (notification as NSNotification).userInfo {
        let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
        let duration:TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
        let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
        let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIViewAnimationOptions().rawValue
        let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
        if (endFrame?.origin.y)! >= UIScreen.main.bounds.size.height {
            scrollViewBottomConstraint?.constant = 0
        } else {
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                let keyboardHeight:Int = Int(keyboardSize.height)
                scrollViewBottomConstraint?.constant = CGFloat(keyboardHeight)
                scrollView.setContentOffset(CGPoint(x: 0, y: (scrollViewBottomConstraint?.constant)! / 2), animated: true)
            }
        }
        UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
            self.view.layoutIfNeeded()
        }, completion: nil)
    }
}

将此项添加到ViewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)

不要忘记将scrollView的底部约束连接到类(名称为“scrollViewBottomConstraint”)。

Swift 4完整解决方案。

我在所有需要它的项目中都使用它

在视图上,将显示“注册”以侦听键盘显示/隐藏,并使用以下功能上下移动视图

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    subscribeToKeyboardNotifications()
}

// stop listening for changes when view is dissappearing
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    unsubscribeFromKeyboardNotifications()
}

// listen for keyboard show/show events
func subscribeToKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: .UIKeyboardWillHide, object: nil)
}

func unsubscribeFromKeyboardNotifications() {
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillHide(_ notification: Notification) {
    view.frame.origin.y = 0
}
在我的例子中,我在底部有一个文本字段,它被键盘隐藏,所以如果这是在使用中,那么我会向上移动视图

@objc func keyboardWillShow(_ notification: Notification) {
    if bottomTextField.isFirstResponder {
        view.frame.origin.y = -getKeyboardHeight(notification: notification)
    }
}

func getKeyboardHeight(notification: Notification) -> CGFloat {
    let userInfo = notification.userInfo
    let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
    return keyboardSize.cgRectValue.height
}

看看这个谢谢@BoilingLime,它很有效。:-)