Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

space between textView and keyboard

I am facing an issue, whenever I going to type in ALTextInputBar() there is a space between keyboard and ALTextInputBar() of 44 points. I don't know from where it is coming. Please have a look on code and image.

@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

like image 674
Ketan Shinde Avatar asked Sep 07 '25 13:09

Ketan Shinde


1 Answers

Late answer but may help someone. I had the same issue and found this below code in the documentation.

Only helpful for IQKeyboardManager users.

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.
like image 61
Usman Avatar answered Sep 09 '25 15:09

Usman