使用交互式键盘关闭时无法获取键盘高度 [英] Can't get keyboard height when using Interactive keyboard dismissal
问题描述
我一直在尝试实现类似"iMessages"应用程序中的交互式键盘.
I have been trying to implement interactive keyboard like in 'iMessages' app Something like this;
上下滑动时,我需要不断获得键盘的准确框架.
I need to continuously get exact frame of keyboard when sliding it up or down.
我已经尝试过了;
keyboardWillChangeFrame,keyboardDidChangeFrame,keyboardWillShowForResizing,keyboardWillHideForResizing,keyboardWillShow,keyboardWillBeHidden
没有一个可以连续返回帧.捕捉该帧的最佳方法是什么?
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?
推荐答案
这很棘手&很简单.
That's a tricky & Simple.
ViewController继承了 UIResponder
ViewController inherits UIResponder
打开类UIViewController:UIResponder,NSCoding,UIAppearanceContainer,UITraitEnvironment,UIContentContainer,UIFocusEnvironment
因为它也可以输入附件视图,所以它可以成为第一响应者.
So it can become first responder since it can have input accessory view too.
您要做的是
1)转到情节提要-> ViewController,然后选择表格视图并将键盘关闭类型更改为
1) Go to Storyboard -> ViewController and then select your tableview and change keyboard dismiss type to
2)在您的Viewcontroller中
2) In your Viewcontroller
添加以下代码
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))
}
只要奔跑就爆炸!!
这篇关于使用交互式键盘关闭时无法获取键盘高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!