如何在键盘上方添加按钮

Jov*_*vic 2 button ios swift2 swift3 ios10

如何在Stack Exchange应用程序中添加键盘上方的按钮?当您长按UITextView中的文本时如何添加"选择"和"全选"?

air*_*aft 8

第一个问题,你可以设置textFieldinputAccessoryView,以自定义视图,这样可以自定义键盘的头.

结果:

在此输入图像描述

你可以像下面这样做;

首先,您应该在键盘上方添加要添加的视图.

在此输入图像描述

class ViewController : UIViewController {

@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

    textField.inputAccessoryView = Bundle.main.loadNibNamed("CustomAccessoryView", owner: self, options: nil)?.first as! UIView?
Run Code Online (Sandbox Code Playgroud)

在你的CustomAccessoryView,你可以设置按钮的动作:

import UIKit

class CustomAccessoryView: UIView {

    @IBAction func clickLoveButton(_ sender: UIButton) {

        print("Love button clicked")
    }
}
Run Code Online (Sandbox Code Playgroud)


dir*_*nee 5

我建议toolbar为您UITextField的附件视图属性创建一个。

想法是toolbar在文本字段首次显示之前将其添加一次。因此,我们将分配delegate给self,并在textFieldShouldBeginEditing实现中覆盖委托调用以添加accessoryView

这是一个简单的示例,您如何实现它:

import UIKit

class ViewController: UIViewController {
    // your `UITextfield` instance
    // Don't forget to attach it from the IB or create it programmaticly
    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Assign the delegate to self
        textField.delegate = self
    }
}

// MARK: Create extension to conform to UITextfieldDelegate
extension ViewController: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        setupTextFieldsAccessoryView()
        return true
    }

    func setupTextFieldsAccessoryView() {
        guard textField.inputAccessoryView == nil else {
            print("textfields accessory view already set up")
            return
        }

        // Create toolBar
        let toolBar: UIToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
        toolBar.barStyle = UIBarStyle.black
        toolBar.isTranslucent = false

        // Add buttons as `UIBarButtonItem` to toolbar
        // First add some space to the left hand side, so your button is not on the edge of the screen
        let flexsibleSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) // flexible space to add left end side

        // Create your first visible button
        let doneButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(didPressDoneButton))
        // Note, that we declared the `didPressDoneButton` to be called, when Done button has been pressed
        toolBar.items = [flexsibleSpace, doneButton]

        // Assing toolbar as inputAccessoryView
        textField.inputAccessoryView = toolBar
    }

    func didPressDoneButton(button: UIButton) {
        // Button has been pressed
        // Process the containment of the textfield or whatever

        // Hide keyboard
        textField.resignFirstResponder()
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该是您的输出:

在此处输入图片说明