пространство между textView и клавиатурой - PullRequest
0 голосов
/ 30 декабря 2018

Я сталкиваюсь с проблемой, всякий раз, когда я набираю 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

...