最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift获取键盘显示之前的事件

iOS crifan 2246浏览 0评论

已有一个页面

里面很多行cell

其中有一些是可以编辑的cell,是Textfield或Textview:

现在的问题是,点击有些行,在页面偏下的位置,

键盘出现后,就遮挡住了:

比如点击 介绍人

而之前自己也已经实现了一个

AutoMoveUpViewController

可以通过监听键盘出现的时候,去自动将页面向上移动

对应的输入框,就不会被遮挡住了

但是需要指定对应的最底部的view

所以希望实现:

在键盘显示之前,就可以获得对应的事件,然后传递过去对应的view

swift before keyboard show event

swift before keyboard show

ios – How to detect when keyboard is shown and hidden – Stack Overflow

去看看是否有:

UIKeyboardWillShowNotification

在此之前的事件

有:

public let UIKeyboardWillShowNotification: String
public let UIKeyboardDidShowNotification: String
public let UIKeyboardWillHideNotification: String
public let UIKeyboardDidHideNotification: String

实在不行就去:

此处用UIKeyboardDidShowNotification,然后再去调整界面

然后此时就可以去利用UIKeyboardWillShowNotification,去传递对应的view了。。

先去找找别人方案

看看textfield被点击选中时候,未获得焦点之前,有哪些事件

public protocol UITextFieldDelegate : NSObjectProtocol {
    @available(iOS 2.0, *)
    optional public func textFieldShouldBeginEditing(textField: UITextField) -> Bool // return NO to disallow editing.
    @available(iOS 2.0, *)
    optional public func textFieldDidBeginEditing(textField: UITextField) // became first responder

去试试:

textFieldShouldBeginEditing

textFieldDidBeginEditing

看看两个是否有在:

UIKeyboardWillShowNotification

之前就执行的。

经过测试,对应事件执行顺序是:

textFieldShouldBeginEditing -> textFieldDidBeginEditing -> UIKeyboardWillShowNotification

所以,用:

textFieldDidBeginEditing比较合适

[后记]

[已解决]keyboardWillShow中改变了self.view.frame.origin.y但是页面并没有先上移动

转载请注明:在路上 » [已解决]swift获取键盘显示之前的事件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.188 seconds, using 22.07MB memory