使TextField的UIToolbar向上移动键盘
问题描述:
我有一个带有TextField和Button的UIToolbar作为UIBarButtonItem。当用户点击工具栏内的TextField时,我试图将此工具栏用作键盘的inputAccessory。使TextField的UIToolbar向上移动键盘
我发现this question,试图解决同样的问题。不幸的是,解决方案并不有效。
我想要的是:
class ChatViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var chatTableView: UITableView!
@IBOutlet weak var chatToolbar: UIToolbar!
@IBOutlet weak var textFieldBarButtonItem: UIBarButtonItem!
override func viewDidAppear(animated: Bool) {
super.viewDidLoad()
self.chatTableView.delegate = self
self.chatTableView.dataSource = self
self.chatToolbar.removeFromSuperview()
}
override var inputAccessoryView: UIView{
get{
return self.chatToolbar
}
}
override func canBecomeFirstResponder() -> Bool {
return true
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell()
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
}
什么我得到的回复是:
*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason:
'child view controller:<UICompatibilityInputViewController: 0x13ff34e00> should have parent view controller:
<App.ChatViewController: 0x13ff21cc0> but requested parent is:<UIInputWindowController: 0x1400b4600>'
任何想法?
答
首先第一件事情:
- 你应该在故事板创建约束工具栏(左,下,右)。
- 在视图控制器中创建底部约束的出口(从故事板拖动)。保存最初的约束条件值(当键盘消失恢复)
- 当你的键盘出现和消失创建一个观察者知道(3.1,并创建一个敲击姿态隐藏键盘)
- 当键盘4.1出现和4.2消失了,你会只改变底部约束值(键盘大小)。您也可以为工具栏设置动画。
喜欢的东西:
class ChatViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var chatTableView: UITableView!
@IBOutlet weak var chatToolbar: UIToolbar!
@IBOutlet weak var textFieldBarButtonItem: UIBarButtonItem!
//2
@IBOutlet weak var toolbarBottomConstraint: NSLayoutConstraint!
var toolbarBottomConstraintInitialValue: CGFloat?
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.chatTableView.delegate = self
self.chatTableView.dataSource = self
self.chatToolbar.removeFromSuperview()
//2
self.toolbarBottomConstraintInitialValue = toolbarBottomConstraint.constant
//3
enableKeyboardHideOnTap()
}
// 3
// Add a gesture on the view controller to close keyboard when tapped
private func enableKeyboardHideOnTap(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil) // See 4.1
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil) //See 4.2
// 3.1
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "hideKeyboard")
self.view.addGestureRecognizer(tap)
}
//3.1
func hideKeyboard() {
self.view.endEditing(true)
}
//4.1
func keyboardWillShow(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
UIView.animateWithDuration(duration) {() -> Void in
self.toolbarBottomConstraint.constant = keyboardFrame.size.height + 5
self.view.layoutIfNeeded()
}
}
//4.2
func keyboardWillHide(notification: NSNotification) {
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
UIView.animateWithDuration(duration) {() -> Void in
self.toolbarBottomConstraint.constant = self.toolbarBottomConstraintInitialValue!
self.view.layoutIfNeeded()
}
}
override var inputAccessoryView: UIView{
get{
return self.chatToolbar
}
}
override func canBecomeFirstResponder() -> Bool {
return true
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell()
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
}
希望它能帮助!
+0
工作就像一个魅力。你是男人。 – adolfosrs
+0
工作完美! –
这不是一个坏的方法,问题应该是'inputAccessoryView:'在'viewDidAppear'之前被调用。附件视图不应该属于任何视图层次结构。顺便说一句:接受的答案也没关系 – Omer
@Omer太棒了,你有什么建议? – adolfosrs
我认为这两种方法都很好,我更喜欢accessoryInputView,因为它在其他好处(如:更简单的使用'UIScrollViewKeyboardDismissModeInteractive',故事板/独立于xib,约束疯狂free = D的方法)之间更干净。下面的链接包含一个obj-c proj,其基本实现使用了可以演变的附件视图方法,以防万一您想尝试一下:https://www.dropbox.com/s/a4bsvz0ie95i3qn/KeyboardAccessory.zip? dl = 0 |||不是我使用工具栏,但你可以使用任何你想要的UIView子类 – Omer