【问题标题】:Move view when keyboard appears and hide again when tapped around当键盘出现时移动视图并在点击时再次隐藏
【发布时间】:2019-10-02 16:55:34
【问题描述】:

我有NSLayoutConstraint 类的以下代码。该课程帮助我在键盘出现时移动视图。没关系,它有效。

但是不知何故,当我将这个类分配给一个约束并且键盘是可见的按钮不能被点击,只要它是可见的。

我经历过这是因为我有另一种代码和平,当敲击时隐藏键盘 - 一旦我移除它,我可以在键盘可见时点击按钮,但这带来了另一个问题,即键盘被点击时不会消失。

有没有办法将两者结合起来,以便视图移动,但如果我四处点击,键盘就会再次消失。

当被点击时:

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

移动视图:

import UIKit

#if !os(tvOS)
@available(tvOS, unavailable)
public class KeyboardLayoutConstraint: NSLayoutConstraint {

    private var offset : CGFloat = 0
    private var keyboardVisibleHeight : CGFloat = 0

    @available(tvOS, unavailable)
    override public func awakeFromNib() {
        super.awakeFromNib()

        offset = constant

        NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: UIWindow.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillHideNotification(_:)), name: UIWindow.keyboardWillHideNotification, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    // MARK: Notification

    @objc func keyboardWillShowNotification(_ notification: Notification) {
        if let userInfo = notification.userInfo {
            if let frameValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
                let frame = frameValue.cgRectValue
                keyboardVisibleHeight = frame.size.height / 2
            }

            self.updateConstant()
            switch (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber) {
            case let (.some(duration), .some(curve)):

                let options = UIView.AnimationOptions(rawValue: curve.uintValue)

                UIView.animate(
                    withDuration: TimeInterval(duration.doubleValue),
                    delay: 0,
                    options: options,
                    animations: {
                        UIApplication.shared.keyWindow?.layoutIfNeeded()
                        return
                }, completion: { finished in
                })
            default:

                break
            }

        }

    }

    @objc func keyboardWillHideNotification(_ notification: NSNotification) {
        keyboardVisibleHeight = 0
        self.updateConstant()

        if let userInfo = notification.userInfo {

            switch (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber, userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber) {
            case let (.some(duration), .some(curve)):

                let options = UIView.AnimationOptions(rawValue: curve.uintValue)

                UIView.animate(
                    withDuration: TimeInterval(duration.doubleValue),
                    delay: 0,
                    options: options,
                    animations: {
                        UIApplication.shared.keyWindow?.layoutIfNeeded()
                        return
                }, completion: { finished in
                })
            default:
                break
            }
        }
    }

    func updateConstant() {
        self.constant = (offset - keyboardVisibleHeight)
    }

}
#endif

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    如果您可以使用第三方库,请结帐:

    https://github.com/hackiftekhar/IQKeyboardManager

    这个库非常有用且高度可定制,可以立即完成工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-23
      • 2018-06-19
      • 2023-03-08
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2016-05-07
      相关资源
      最近更新 更多