swift:为键盘添加自定义工具栏

有时候需要为键盘添加一些自定义的按键,如“上一步”,“下一眇”,“完成” 按钮等。

首先自定义一个toolbar

func addToolbar() -> UIToolbar {
var toolbar: UIToolbar = UIToolbar();
toolbar.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 35)
toolbar.tintColor = UIColor.blue;
toolbar.backgroundColor = UIColor.gray;
var pre: UIBarButtonItem = UIBarButtonItem(title: “上一步”, style: UIBarButtonItemStyle.plain, target: self, action: #selector(textFieldpre))
var next: UIBarButtonItem = UIBarButtonItem(title: “下一步”, style: UIBarButtonItemStyle.plain, target: self, action: #selector(textFieldnext))

var space: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, …