Berk Kaya
Berk Kaya

Reputation: 470

Can't get keyboard height when using Interactive keyboard dismissal

I have been trying to implement interactive keyboard like in 'iMessages' app Something like this;
This

I need to continuously get exact frame of keyboard when sliding it up or down.

I have already tried;
keyboardWillChangeFrame, keyboardDidChangeFrame, keyboardWillShowForResizing, keyboardWillHideForResizing, keyboardWillShow, keyboardWillBeHidden

None of them continuously return the frame. What is the best way to catch that frame?

Upvotes: 1

Views: 981

Answers (1)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16416

That's a tricky & Simple.

ViewController inherits UIResponder

open class UIViewController : UIResponder, NSCoding, UIAppearanceContainer, UITraitEnvironment, UIContentContainer, UIFocusEnvironment

So it can become first responder since it can have input accessory view too.

What you have to do is

1) Go to Storyboard -> ViewController and then select your tableview and change keyboard dismiss type to

enter image description here

2) In your Viewcontroller

Add following code

var viewAcc : UIView? // That contains TextFied and send button

override var inputAccessoryView: UIView? {
    return viewAcc
}



override var canBecomeFirstResponder: Bool {
    return true
}

For you I have created simple Textfield and send button as example you can modify it with your's

 func setupView () {


    self.tblChatDetail.contentInset = UIEdgeInsetsMake(0, 0, 20, 0)


    viewAcc = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
    viewAcc?.backgroundColor = UIColor.white

    textView = UITextView (frame: CGRect(x:0, y:0, width:0,height: 44 - 0.5))
    textView.textContainerInset = UIEdgeInsetsMake(4, 3, 3, 3)
    textView.delegate = self
    viewAcc?.backgroundColor = .lightGray


    viewAcc?.addSubview(textView);

    sendButton = UIButton(type: .system)
    sendButton.isEnabled = true
    sendButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
    sendButton.setTitle("Send", for: .normal)
    sendButton.contentEdgeInsets = UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 6)
    sendButton.addTarget(self, action: #selector(ChatDetailViewController.sendMessage), for: UIControlEvents.touchUpInside)
    viewAcc?.addSubview(sendButton)


    textView.translatesAutoresizingMaskIntoConstraints = false
    sendButton.translatesAutoresizingMaskIntoConstraints = false
    viewAcc?.addConstraint(NSLayoutConstraint(item: textView, attribute: .left, relatedBy: .equal, toItem: viewAcc, attribute: .left, multiplier: 1, constant: 8))
    viewAcc?.addConstraint(NSLayoutConstraint(item: textView, attribute: .top, relatedBy: .equal, toItem: viewAcc, attribute: .top, multiplier: 1, constant: 7.5))
    viewAcc?.addConstraint(NSLayoutConstraint(item: textView, attribute: .right, relatedBy: .equal, toItem: sendButton, attribute: .left, multiplier: 1, constant: -2))
    viewAcc?.addConstraint(NSLayoutConstraint(item: textView, attribute: .bottom, relatedBy: .equal, toItem: viewAcc, attribute: .bottom, multiplier: 1, constant: -8))
    viewAcc?.addConstraint(NSLayoutConstraint(item: sendButton, attribute: .right, relatedBy: .equal, toItem: viewAcc, attribute: .right, multiplier: 1, constant: 0))
    viewAcc?.addConstraint(NSLayoutConstraint(item: sendButton, attribute: .bottom, relatedBy: .equal, toItem: viewAcc, attribute: .bottom, multiplier: 1, constant: -4.5))


}

Just run and Bang on !!

Upvotes: 1

Related Questions