键盘弹出时如何移动整个视图? (迅速)

时间:2015-04-26 11:42:22

标签: swift

enter image description here

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

当键盘弹出时,我应该添加哪些功能才能向上移动整个视图?

4 个答案:

答案 0 :(得分:2)

要检测键盘何时显示,您可以收听NSNotificationCenter

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: “keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

这将调用keyboardWillShowkeyboardWillHide。在这里,您可以使用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
}

答案 1 :(得分:1)

正如Milo所说,要自己注册键盘显示/隐藏通知。

然后,您需要编写代码来确定键盘隐藏的屏幕数量,以及相关字段在屏幕上显示的位置,以便了解移动视图的数量。

一旦你做完了,你做的事情取决于你是否使用AutoLayout或自动调整面具(a.k.a。" Struts和spring"样式布局。)

我写了一篇关于项目的开发者博客文章,其中包括用于移动键盘的工作代码。看到这个链接:

http://wareto.com/animating-shapes-using-cashapelayer-and-cabasicanimation

在该帖子中,查找标题为“#34;滑动视图以为键盘腾出空间”的链接"在底部。

答案 2 :(得分:1)

Swift 4完整解决方案。

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

在viewWillAppear寄存器上监听键盘显示/隐藏,并使用下面的功能向上和向下移动视图。

<a class="text-muted" href="{{ path('logout') }}">logout </a>

在我的情况下,我在底部有一个文本字段,它被键盘隐藏,所以如果使用它,那么我将视图向上移动

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
}

答案 3 :(得分:-1)

Swift 4

此代码不是那么完美,但值得一试!

首先将整个视图嵌入到scrollView中。

将这个小可爱功能添加到你的班级:

@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的底部约束连接到您的班级(名称为&#34; scrollViewBottomConstraint&#34;)。