ios - 数据输入自定义 View 的 Swift 示例(自定义应用内键盘)

标签 ios swift keyboard custom-keyboard

目标

我想制作一个只在我的应用程序中使用的自定义键盘,而不是需要安装的系统键盘。

我读过和尝试过的内容

文档

上面第一篇文章说:

Make sure a custom, systemwide keyboard is indeed what you want to develop. To provide a fully custom keyboard for just your app or to supplement the system keyboard with custom keys in just your app, the iOS SDK provides other, better options. Read about custom input views and input accessory views in Custom Views for Data Input in Text Programming Guide for iOS.

这就是让我看到上面第二篇文章的原因。但是,那篇文章没有足够的细节让我开始。

教程

我能够从上面列表中的第二个教程中获得一个可用的键盘。但是,我找不到任何教程来展示如何制作一个应用内专用键盘,如 Custom Views for Data Input 中所述。文档。

堆栈溢出

在回答当前问题的过程中,我也问了(并回答了)这些问题。

问题

有没有人有应用内自定义键盘的最小示例(甚至只有一个按钮)?我不是在寻找一个完整的教程,只是一个我可以自己扩展的概念证明。

最佳答案

enter image description here

这是一个基本的应用内键盘。几乎可以使用相同的方法来制作任何键盘布局。以下是需要完成的主要工作:

  • 在 .xib 文件中创建键盘布局,其所有者是一个包含 UIView 子类的 .swift 文件。
  • 告诉 UITextField 使用自定义键盘。
  • 使用委托(delegate)在键盘和主视图 Controller 之间进行通信。

创建 .xib 键盘布局文件

  • 在 Xcode 中转到 File > New > File... > iOS > User Interface > View 创建 .xib 文件。
  • 我调用了我的 Keyboard.xib
  • 添加您需要的按钮。
  • 使用自动布局约束,这样无论键盘大小如何,按钮都会相应地调整大小。
  • 将文件的所有者(不是 Root View )设置为 Keyboard.swift 文件。这是一个常见的错误来源。请参阅末尾的注释。

创建 .swift UIView 子类键盘文件

  • 在 Xcode 中转到 File > New > File... > iOS > Source > Cocoa Touch Class 创建 .swift 文件。
  • 我叫我的 Keyboard.swift
  • 添加以下代码:

    import UIKit
    
    // The view controller will adopt this protocol (delegate)
    // and thus must contain the keyWasTapped method
    protocol KeyboardDelegate: class {
        func keyWasTapped(character: String)
    }
    
    class Keyboard: UIView {
    
        // This variable will be set as the view controller so that 
        // the keyboard can send messages to the view controller.
        weak var delegate: KeyboardDelegate?
    
        // MARK:- keyboard initialization
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            initializeSubviews()
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            initializeSubviews()
        }
    
        func initializeSubviews() {
            let xibFileName = "Keyboard" // xib extention not included
            let view = Bundle.main.loadNibNamed(xibFileName, owner: self, options: nil)![0] as! UIView
            self.addSubview(view)
            view.frame = self.bounds
        }
    
        // MARK:- Button actions from .xib file
    
        @IBAction func keyTapped(sender: UIButton) {
            // When a button is tapped, send that information to the 
            // delegate (ie, the view controller)
            self.delegate?.keyWasTapped(character: sender.titleLabel!.text!) // could alternatively send a tag value
        }
    
    }
    
  • 控制从 .xib 文件中的按钮拖动到 .swift 文件中的 @IBAction 方法以将它们全部连接起来。

  • 注意协议(protocol)和委托(delegate)代码。参见 this answer获取有关代表如何工作的简单说明。

设置 View Controller

  • UITextField 添加到您的主 Storyboard,并使用 IBOutlet 将其连接到您的 View Controller 。称它为 textField
  • 为 View Controller 使用以下代码:

    import UIKit
    
    class ViewController: UIViewController, KeyboardDelegate {
    
        @IBOutlet weak var textField: UITextField!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // initialize custom keyboard
            let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
            keyboardView.delegate = self // the view controller will be notified by the keyboard whenever a key is tapped
    
            // replace system keyboard with custom keyboard
            textField.inputView = keyboardView
        }
    
        // required method for keyboard delegate protocol
        func keyWasTapped(character: String) {
            textField.insertText(character)
        }
    }
    
  • 请注意, View Controller 采用了我们在上面定义的 KeyboardDelegate 协议(protocol)。

常见错误

如果您收到 EXC_BAD_ACCESS 错误,可能是因为您将 View 的自定义类设置为 Keyboard.swift 而不是为 nib 文件的所有者执行此操作。

选择 Keyboard.nib,然后选择文件所有者。

enter image description here

确保 Root View 的自定义类为空。

enter image description here

关于ios - 数据输入自定义 View 的 Swift 示例(自定义应用内键盘),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33474771/

相关文章:

xcode - 布局中的奇怪异常

python - 键盘在 PyCharm IDE 中不工作,光标不出现在任何地方

ios - Alamofire 错误 token (发布)与 Swift

ios - 快速的单选按钮

iphone - iOS:我可以让 UITableView 在 iPhone 横向模式下水平滚动吗?

android - Android、iPhone、BlackBerry常用加解密算法

JavaFX KEY_TYPED 事件没有执行任何操作

crash - iOS9 关闭键盘导致崩溃

ios - TableViewCell 中用于打开 map 应用的按钮

ios - 我的应用程序因 Flurry 崩溃