答案 0 :(得分:36)
这是使用扩展程序的 Swift 3 解决方案。非常适合您的应用中有多个数字UITextField
对象,因为它可以灵活地为每个UITextField
决定在完成或时执行自定义操作点击取消。
//
// UITextField+DoneCancelToolbar.swift
//
import UIKit
extension UITextField {
func addDoneCancelToolbar(onDone: (target: Any, action: Selector)? = nil, onCancel: (target: Any, action: Selector)? = nil) {
let onCancel = onCancel ?? (target: self, action: #selector(cancelButtonTapped))
let onDone = onDone ?? (target: self, action: #selector(doneButtonTapped))
let toolbar: UIToolbar = UIToolbar()
toolbar.barStyle = .default
toolbar.items = [
UIBarButtonItem(title: "Cancel", style: .plain, target: onCancel.target, action: onCancel.action),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil),
UIBarButtonItem(title: "Done", style: .done, target: onDone.target, action: onDone.action)
]
toolbar.sizeToFit()
self.inputAccessoryView = toolbar
}
// Default actions:
func doneButtonTapped() { self.resignFirstResponder() }
func cancelButtonTapped() { self.resignFirstResponder() }
}
使用默认操作的使用示例:
//
// MyViewController.swift
//
@IBOutlet weak var myNumericTextField: UITextField! {
didSet { myNumericTextField?.addDoneCancelToolbar() }
}
使用自定义完成操作的使用示例:
//
// MyViewController.swift
//
@IBOutlet weak var myNumericTextField: UITextField! {
didSet {
myNumericTextField?.addDoneCancelToolbar(onDone: (target: self, action: #selector(doneButtonTappedForMyNumericTextField)))
}
}
func doneButtonTappedForMyNumericTextField() {
print("Done");
myNumericTextField.resignFirstResponder()
}
答案 1 :(得分:18)
此解决方案适用于我=)
来源:https://gist.github.com/jplazcano87/8b5d3bc89c3578e45c3e
我为UITextField做了一个扩展,现在我可以在我想要的所有键盘上都有一个“Done Bar”。
extension UITextField{
func addDoneButtonToKeyboard(myAction:Selector?){
let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 300, height: 40))
doneToolbar.barStyle = UIBarStyle.default
let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: myAction)
var items = [UIBarButtonItem]()
items.append(flexSpace)
items.append(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
self.inputAccessoryView = doneToolbar
}
}
MyViewController
class myViewController:UIViewController,UITextFieldDelegate{
//MARK: - Outlets
@IBOutlet weak var myTextField: UITextField!
//MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
//You can specify your own selector to be send in "myAction"
myTextField.addDoneButtonToKeyboard(myAction: #selector(self.myTextField.resignFirstResponder))
}
}
答案 2 :(得分:1)
class ViewController: UIViewController {
let textField = UITextField(frame: CGRect(x: 0, y: 20, width: 200, height: 60))
override func viewDidLoad() {
super.viewDidLoad()
textField.borderStyle = .Line
textField.inputAccessoryView = accessoryView()
textField.inputAccessoryView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
view.addSubview(textField)
}
func accessoryView() -> UIView {
let view = UIView()
view.backgroundColor = UIColor.redColor()
let doneButton = UIButton()
doneButton.frame = CGRect(x: self.view.frame.width - 80, y: 7, width: 60, height: 30)
doneButton.backgroundColor = UIColor.greenColor()
doneButton.setTitle("done", forState: .Normal)
doneButton.addTarget(self, action: #selector(ViewController.doneAction), forControlEvents: .TouchUpInside)
view.addSubview(doneButton)
return view
}
@objc func doneAction() {
textField.resignFirstResponder()
}
}
答案 3 :(得分:0)
为 Swift 4.2 更新@olito答案 对于swift 4选择器,必须是objc方法,因此我们必须在扩展内的函数中添加 @objc
extension UITextField {
func addDoneCancelToolbar(onDone: (target: Any, action: Selector)? = nil, onCancel: (target: Any, action: Selector)? = nil) {
let onCancel = onCancel ?? (target: self, action: #selector(cancelButtonTapped))
let onDone = onDone ?? (target: self, action: #selector(doneButtonTapped))
let toolbar: UIToolbar = UIToolbar()
toolbar.barStyle = .default
toolbar.items = [
UIBarButtonItem(title: "Cancel", style: .plain, target: onCancel.target, action: onCancel.action),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil),
UIBarButtonItem(title: "Done", style: .done, target: onDone.target, action: onDone.action)
]
toolbar.sizeToFit()
self.inputAccessoryView = toolbar
}
// Default actions:
@objc func doneButtonTapped() { self.resignFirstResponder() }
@objc func cancelButtonTapped() { self.resignFirstResponder() }}
(确保连接IBOutlet)
@IBOutlet weak var vehicleModelTF: UITextField!
在viewDidLoad中,我们可以将工具栏添加到文本字段。
myTextField.addDoneCancelToolbar(onDone: (target: self, action: #selector(self.tapDone)), onCancel: (target: self, action: #selector(self.tapCancel)))
现在我们必须添加2个objc方法。
@objc func tapDone() {
print("tapped Done")
}
@objc func tapCancel() {
print("tapped cancel")
}