当 UITableView 嵌入容器视图时 IQKeyboardManager 不工作
IQKeyboardManager not working when UITableView embedded in a container view
目前,我正在使用带有嵌入式 UITableView
的容器视图,并使用 IQKeyboardManager
CocoaPod 滚动视图,因此我的 UITextFields
和 UITextViews
是不被键盘覆盖。
我可以成功 import IQkeyboardManager
并使其在其他视图中工作,但是当 UITableView
嵌入到容器视图中时它不起作用。
尝试滚动所有视图
在 viewDidLoad 中
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourVC.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourVC.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil)
及之后
func keyboardWillShow(notification:NSNotification)
{
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
{
self.view.frame.origin.y -= keyboardSize.height
}
}
func keyboardWillHide(notification:NSNotification)
{
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
{
self.view.frame.origin.y += keyboardSize.height
}
}
你可以改变keyboardSize
我遇到了类似的问题,并使用图书馆作者 here 提供的信息修复了它。
关键语句是:
library logic is to find nearest scrollView from textField. and in your case it's tableView, that is why library chooses tableView to scroll.
所以我使用的解决方案是在编辑 textfield/view 时禁用 UITableView 滚动 属性(使用委托方法),然后在编辑完成后重新启用它.这确保库不会将 UITableView 检测为可滚动,因此它会忽略它,然后按照您的预期移动您的容器视图。视图按照您的意愿向上移动后,您可以通过 UIKeyboardWillShowNotification 重新启用滚动。
例如,对于 UITextField:
-(void) textFieldDidBeginEditing:(UITextField *)textField {
[self.tableView setScrollEnabled:NO];
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
[self.tableView setScrollEnabled:YES];
}
但是为了在视图向上移动后仍然允许滚动,我已经注册了键盘通知,然后在键盘启动后允许滚动:
-(void) keyboardWillShow {
[self.tableView setScrollEnabled:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES]; // This line is needed for the 'auto slide up'
// Do other stuff
}
简单的解决方案,无需创建观察者。
感谢@vivek agravat 解答!
这是 swift 版本:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}
目前,我正在使用带有嵌入式 UITableView
的容器视图,并使用 IQKeyboardManager
CocoaPod 滚动视图,因此我的 UITextFields
和 UITextViews
是不被键盘覆盖。
我可以成功 import IQkeyboardManager
并使其在其他视图中工作,但是当 UITableView
嵌入到容器视图中时它不起作用。
尝试滚动所有视图
在 viewDidLoad 中
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourVC.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourVC.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil)
及之后
func keyboardWillShow(notification:NSNotification)
{
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
{
self.view.frame.origin.y -= keyboardSize.height
}
}
func keyboardWillHide(notification:NSNotification)
{
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
{
self.view.frame.origin.y += keyboardSize.height
}
}
你可以改变keyboardSize
我遇到了类似的问题,并使用图书馆作者 here 提供的信息修复了它。
关键语句是:
library logic is to find nearest scrollView from textField. and in your case it's tableView, that is why library chooses tableView to scroll.
所以我使用的解决方案是在编辑 textfield/view 时禁用 UITableView 滚动 属性(使用委托方法),然后在编辑完成后重新启用它.这确保库不会将 UITableView 检测为可滚动,因此它会忽略它,然后按照您的预期移动您的容器视图。视图按照您的意愿向上移动后,您可以通过 UIKeyboardWillShowNotification 重新启用滚动。
例如,对于 UITextField:
-(void) textFieldDidBeginEditing:(UITextField *)textField {
[self.tableView setScrollEnabled:NO];
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
[self.tableView setScrollEnabled:YES];
}
但是为了在视图向上移动后仍然允许滚动,我已经注册了键盘通知,然后在键盘启动后允许滚动:
-(void) keyboardWillShow {
[self.tableView setScrollEnabled:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES]; // This line is needed for the 'auto slide up'
// Do other stuff
}
简单的解决方案,无需创建观察者。
感谢@vivek agravat 解答! 这是 swift 版本:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}