textView和键盘之间的空间

时间:2018-12-30 10:26:44

标签: swift xcode uitextview uikeyboard swift4.2

我遇到了一个问题,每当我输入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()
}

before typing after typing

1 个答案:

答案 0 :(得分:0)

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

<块引用>

仅对 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.