【问题标题】:Which method is called when my custom keyboard is shown on screen?当我的自定义键盘显示在屏幕上时,会调用哪种方法?
【发布时间】:2017-11-19 17:54:17
【问题描述】:

我开发了自定义键盘。现在我想提供一些功能,比如如果某个用户从我的容器应用程序中选择颜色,然后打开我的键盘,我的键盘的背景颜色会更改为该颜色。如何做到这一点?每次有人切换到我的自定义键盘时都会调用哪个方法,以便我可以在该方法上编写代码?

【问题讨论】:

  • 如果有人认为我的问题是错误的或缺少信息,请提及原因。然后投反对票

标签: ios swift custom-keyboard


【解决方案1】:

没有调用预定义的方法,但您可以设置一个 NSNotification。在您的 viewdidload 方法中插入:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)

然后添加两个方法会调用键盘出现或消失:

func keyboardWasShown(notification: NSNotification) {
    print("keyboard up")
}

func keyboardWillBeHidden(notification: NSNotification) {
    print("keyboard down")
}

但记得删除 viewWillDisappear 中的通知:

NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)

再见。

【讨论】:

    【解决方案2】:

    在 UserDefaults 中使用套件名称将颜色属性保存为十六进制字符串。然后在扩展应用的 viewDidLoad 方法中使用这个颜色作为背景颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 2013-12-10
      相关资源
      最近更新 更多