我在iPad上成功运行以下代码。
func setupLockPinSection() {
let keys = ["1", "2","3", "4","5", "6","7", "8","9", "0", "Clear", "Done"]
let kbFrame = CGRect(x: 0, y: 0, width: view.frame.width, height: 400)
if let customKeyboard = Bundle.main.loadNibNamed("MACustomKeyboard", owner: self, options: nil)?.first as? MACustomKeyboard {
customKeyboard.frame = kbFrame
customKeyboard.setKeyboardButtons(keys)
lockPin1.inputView = customKeyboard
//Number Tapped
customKeyboard.numberTappedBlock = { [weak self] view, text in
if let textFieldText = self?.lockPin1.text, let t = text {
self?.lockPin1.text = textFieldText + t
}
}
//Done button Tapped
customKeyboard.doneTappedBlock = { [weak self] view in
self?.lockPin1.resignFirstResponder()
}
//Clear Button Tapped
customKeyboard.clearTappedBlock = { [weak self] view in
if let text = self?.lockPin1.text, text.characters.count > 0 {
self?.lockPin1.text = text.substring(to: text.index(text.endIndex, offsetBy: -1))
}
}
}
if let customKeyboard = Bundle.main.loadNibNamed("MACustomKeyboard", owner: self, options: nil)?.first as? MACustomKeyboard {
customKeyboard.frame = kbFrame
customKeyboard.setKeyboardButtons(keys)
lockPin2.inputView = customKeyboard
//Number Tapped
customKeyboard.numberTappedBlock = { [weak self] view, text in
if let textFieldText = self?.lockPin2.text, let t = text {
self?.lockPin2.text = textFieldText + t
}
}
//Done button Tapped
customKeyboard.doneTappedBlock = { [weak self] view in
self?.lockPin2.resignFirstResponder()
}
//Clear Button Tapped
customKeyboard.clearTappedBlock = { [weak self] view in
if let text = self?.lockPin2.text, text.characters.count > 0 {
self?.lockPin2.text = text.substring(to: text.index(text.endIndex, offsetBy: -1))
}
}
}
lockPin1.text = UserDefaults.standard.string(forKey: "lockPin")
lockPin2.text = UserDefaults.standard.string(forKey: "lockPin")
}
我想在iOS默认键盘上制作清晰完成的按钮,并显示图像(隐藏键盘按钮,清除按钮)。我想这样做是因为,对于我的锁定功能,我只想使用数字,而在iPad上它不允许这样做。因此我需要制作自定义键盘。