ios – 如何使用应用内自定义键盘的按钮输入文本

前端之家收集整理的这篇文章主要介绍了ios – 如何使用应用内自定义键盘的按钮输入文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我做了一个应用程序内的自定义键盘,取代系统键盘,当我点击UITextField时弹出.

这是我的代码

  1. class ViewController: UIViewController {
  2.  
  3. var myCustomKeyboard: UIView!
  4. @IBOutlet weak var textField: UITextField!
  5.  
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8.  
  9. let keyboardNib = UINib(nibName: "Keyboard",bundle: nil)
  10. myCustomKeyboard = keyboardNib.instantiateWithOwner(self,options: nil)[0] as! UIView
  11.  
  12. textField.inputView = myCustomKeyboard
  13. }
  14.  
  15. }

键盘布局是从xib文件加载的.

如何将按钮文本放入文本字​​段?

笔记:

>有很多关于制作自定义系统键盘的教程(需要安装),但我只想要一个应用内键盘.教程使用一个特殊的视图控制器只为键盘,但在这里似乎我只是设置键盘视图.
>我已阅读Custom Views for Data Input文档.
> This是我能找到的最接近的Stack Overflow问题,但它没有描述如何从按钮获取文本.

更新

> This tutorial似乎表明自定义输入视图有一个视图控制器.但是,我在Objective-C代码中迷失了方向. Swift的过程是什么?
> This answer提到了UITextField符合的UIKeyInput协议,但我该如何使用它?
>如果有任何内置方式也可以制作自定义应用内键盘,我真的更喜欢制作一个普通的自定义视图.

解决方法

建立

>制作包含所有密钥的xib文件
>使用Autolayout,无论键盘设置得多大,按键都会调整到正确的比例.
>创建一个与xib文件同名的swift文件,并在xib文件设置中将其设置为文件所有者.



>将所有关键按钮连接到swift文件中的IBAction方法. (参见下面的代码.)

我正在使用delegate pattern自定义键盘视图和主视图控制器之间进行通信.这允许它们分离.可以交换多个不同的自定义键盘,而无需更改主视图控制器中的详细实现代码.

Keyboard.swift文件

  1. import UIKit
  2.  
  3. protocol KeyboardDelegate {
  4. func keyWasTapped(character: String)
  5. }
  6.  
  7. class Keyboard: UIView {
  8.  
  9. var delegate: KeyboardDelegate?
  10.  
  11. required init?(coder aDecoder: NSCoder) {
  12. super.init(coder: aDecoder)
  13. initializeSubviews()
  14. }
  15.  
  16. override init(frame: CGRect) {
  17. super.init(frame: frame)
  18. initializeSubviews()
  19. }
  20.  
  21. func initializeSubviews() {
  22. let xibFileName = "Keyboard" // xib extention not needed
  23. let view = NSBundle.mainBundle().loadNibNamed(xibFileName,owner: self,options: nil)[0] as! UIView
  24. self.addSubview(view)
  25. view.frame = self.bounds
  26. }
  27.  
  28. @IBAction func keyTapped(sender: UIButton) {
  29. self.delegate?.keyWasTapped(sender.titleLabel!.text!)
  30. }
  31.  
  32. }

主视图控制器

请注意,ViewController符合我们创建的KeyboardDelegate协议.此外,在创建键盘视图的实例时,需要设置高度,但宽度不需要.显然,设置文本字段的inputView会将键盘视图宽度更新为屏幕宽度,这很方便.

  1. class ViewController: UIViewController,KeyboardDelegate {
  2.  
  3. @IBOutlet weak var textField: UITextField!
  4.  
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7.  
  8. // get an instance of the Keyboard (only the height is important)
  9. let keyboardView = Keyboard(frame: CGRect(x: 0,y: 0,width: 0,height: 300))
  10.  
  11. // use the delegate to communicate
  12. keyboardView.delegate = self
  13.  
  14. // replace the system keyboard with the custom keyboard
  15. textField.inputView = keyboardView
  16. }
  17.  
  18. // required method for keyboard delegate protocol
  19. func keyWasTapped(character: String) {
  20. textField.insertText(character)
  21. }
  22.  
  23. }

来源

> @ryancrunchi’s comment的建议很有帮助.
> This answerCreating a reusable UIView with xib (and loading from storyboard)

有关

> A Swift example of Custom Views for Data Input (custom in-app keyboard)

猜你在找的iOS相关文章