KeyboardWillShowNotification 边缘情况
这是操作顺序的问题
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[nc addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
然后我向 UITableViewCell 添加一个文本框:
[textField addTarget:self
action:@selector(textFieldBegin:)
forControlEvents:UIControlEventEditingDidBegin];
[cell addSubview:textField];
在 textFieldBegin 中,我滚动ToRowAtIndexPath 移动到正在编辑的单元格。
在keyboardWillShow中,我调整tableView的框架以允许键盘。
textFieldBegin 在 keyboardWillShow 之前被调用,因此第一次显示时没有滚动空间。
有没有一种有效的方法来解决这个疏忽?
This is a problem of order of operations
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[nc addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
And then I add a textbox to a UITableViewCell:
[textField addTarget:self
action:@selector(textFieldBegin:)
forControlEvents:UIControlEventEditingDidBegin];
[cell addSubview:textField];
In textFieldBegin, I scrollToRowAtIndexPath to move to the cell being edited.
In keyboardWillShow I adjust the frame of the tableView to allow for the keyboard.
textFieldBegin gets called before keyboardWillShow, so the first time it is shown it has no room to scroll.
Is there an elequent way to fix this oversight?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在开始期间将当前滚动位置存储在变量中,然后在键盘将显示通知中,您可以重新滚动到该变量中存储的任何位置。
我认为这可以让您将不同的动画保留在它们所属的位置。
You could store the current scroll position in a variable during begin, then in the keyboardwillshow notification you could re-scroll to whatever position is stored in that variable.
I think that would allow you to keep your different animations where they belong.
您可以对 UITextFieldDelegate 方法
textFieldDidBeginEditing:
做出反应,并在该方法中调整大小/滚动,而不是监听UIKeyboardWillShowNotification
,因为它是在该方法之后调用的。显示键盘。Instead of listening for the
UIKeyboardWillShowNotification
you can react to the UITextFieldDelegate methodtextFieldDidBeginEditing:
and resize/scroll in that method, since it is called after the keyboard is shown.