Ios 使用文本字段使UIToolbar随键盘向上移动
我有一个UIToolbar,带有一个文本字段和一个按钮作为UIBarButtonim。当用户点击工具栏内的文本字段时,我尝试将此工具栏用作键盘的输入附件 我发现它试图解决同样的问题。不幸的是,这一解决办法并不有效 我尝试的是:Ios 使用文本字段使UIToolbar随键盘向上移动,ios,xcode,swift,Ios,Xcode,Swift,我有一个UIToolbar,带有一个文本字段和一个按钮作为UIBarButtonim。当用户点击工具栏内的文本字段时,我尝试将此工具栏用作键盘的输入附件 我发现它试图解决同样的问题。不幸的是,这一解决办法并不有效 我尝试的是: class ChatViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak
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>'
***由于未捕获的异常“UIViewControllerHierarchyConsistency”而终止应用程序,原因:
'子视图控制器:应具有父视图控制器:
但请求的家长是:'
有什么想法吗?第一件事:
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
}
}
希望有帮助 这是一种不错的方法,问题应该是在
视图出现之前调用inputAccessoryView:
。附件视图不应属于任何视图层次。顺便说一句:接受的答案是可以的too@Omer很好,你有什么建议吗?我认为这两种方法都很好,我更喜欢accessoryInputView,因为它在其他优点之间更为简洁(即:更容易使用UIScrollViewKeyboardDismissModeInteractive
,storyboard/xib独立,无约束疯狂=D)。下面的链接包含一个obj-c项目,其中包含一个使用附件视图方法的基本实现,您可以改进它,以防您想尝试一下:| | | |而不是我“我使用工具栏,但你可以使用任何你想要的UIView子类。我的意思是,我应该如何在视图出现后强制调用inputAccessoryView
?不确定你为什么需要它,inputAccessoryView
是UIResponder的一个属性(也就是UIViewController的属性)。此类的基本实现使用此属性,以便在需要时提供自定义行为。检查我共享的项目。