【问题标题】:space between textView and keyboardtextView 和键盘之间的空间
【发布时间】:2018-12-30 10:26:44
【问题描述】:

我遇到了一个问题,每当我要输入 ALTextInputBar() 时,键盘和 ALTextInputBar() 之间有一个 44 点的空格。我不知道它从哪里来。请查看代码和图像。

@IBOutlet weak var viewChatBox: UIView!
@IBOutlet weak var viewChatBoxBottomConstraint: NSLayoutConstraint!

let textInputBar = ALTextInputBar()
let keyboardObserver = ALKeyboardObservingView()


override func viewDidLoad() {
    super.viewDidLoad()

    IQKeyboardManager.shared.enable = false
    IQKeyboardManager.shared.enableAutoToolbar = false

    configureView()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if #available(iOS 11, *) {
        // safe area constraints already set
    }
    else {
        if (!(self.topLayoutConstraint != nil)) {
            topLayoutConstraint = viewTopBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor)
            self.topLayoutConstraint?.isActive = true
        }
        if  (!(self.bottomLayoutConstraint != nil)) {
            //                bottomLayoutConstraint = viewChatBox.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor)
            self.bottomLayoutConstraint?.isActive = true
        }
    }
}

func configureInputBar () {

    btnChat = UIButton(frame: CGRect(x: 0, y: 0, width: 36, height: 36))

    keyboardObserver.isUserInteractionEnabled = false
    textInputBar.showTextViewBorder = true
    textInputBar.leftView = nil
    textInputBar.rightView = btnChat
    textInputBar.alwaysShowRightButton = true
    textInputBar.delegate = self
    textInputBar.textView.autocorrectionType = .yes
    textInputBar.backgroundColor = UIColor(white: 0.95, alpha: 1)
    textInputBar.keyboardObserver = keyboardObserver
    viewChatBox.addSubview(textInputBar)
    applyConstraintToChatBox()
    self.view.layoutIfNeeded()
}


func applyConstraintToChatBox() {

    textInputBar.translatesAutoresizingMaskIntoConstraints = false
    viewChatBox.translatesAutoresizingMaskIntoConstraints = false

    let views = ["textView": textInputBar]
    let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[textView]-0-|", options: [], metrics: nil, views: views)
    let vConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[textView]-0-|", options: [], metrics: nil, views: views)
    viewChatBox.addConstraints(hConstraints)
    viewChatBox.addConstraints(vConstraints)
}

override var inputAccessoryView: UIView? {
    get {
        return keyboardObserver
    }
}

override var canBecomeFirstResponder: Bool {
    return true
}


//MARK: - TEXTVIEW DELEGATE

func textView(textView: ALTextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    if (textInputBar.text.count == 0) {
        return true
    }
    let newLength = textInputBar.text.count + text.count - range.length
    return (newLength <= 144);
}


func inputBarDidChangeHeight(height: CGFloat) {
    UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [.curveLinear], animations: { () -> Void in
        self.view.layoutIfNeeded()
    }, completion: nil)
}

// MARK: - KEYBOARDS

@objc func keyboardWillChangeFrame(notification: Notification) {

    let endFrame = ((notification as NSNotification).userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    viewChatBoxBottomConstraint.constant = view.bounds.height - endFrame.origin.y

    print("CHAT BOX FRAME: \(viewChatBox.frame)")
    print("TEXT FRAME FRAME: \(textInputBar.frame)")

    self.view.layoutIfNeeded()
}

【问题讨论】:

  • 同样的空间也有同样的问题。你解决了吗?
  • 我发现如果您使用navigationController 并在底部有一个工具栏,那么您将在键盘上方有那个空间。因为它在我调用 `navigationController?.isToolbarHidden = true` 后消失了。
  • 这个问题你解决了吗,我也一样。

标签: swift xcode uitextview uikeyboard swift4.2


【解决方案1】:

迟到的答案,但可能对某人有所帮助。我遇到了同样的问题,并在文档中找到了以下代码。

仅对 IQKeyboardManager 用户有帮助。

IQKeyboardManager

self.textField.keyboardDistanceFromTextField = 8; //This will modify default distance between textField and keyboard. For exact value, please manually check how far your textField from the bottom of the page. Mine was 8pt.

【讨论】: