如何在键盘上方添加按钮

问题描述 投票:1回答:4

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

ios button swift2 swift3 ios10
4个回答
7
投票

第一个问题,您可以将textFieldinputAccessoryView设置为自定义视图,这可以自定义键盘的标题。

结果:

enter image description here

你可以像下面这样做;

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

enter image description here

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?

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

import UIKit

class CustomAccessoryView: UIView {

    @IBAction func clickLoveButton(_ sender: UIButton) {

        print("Love button clicked")
    }
}

5
投票

我建议为你的toolbar的accessoryView属性创建一个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()
    }
}

这应该是你的输出:

enter image description here


1
投票

只需复制并粘贴嵌入键盘的附件按钮的简单代码即可

func addKeyboardToolbar()  {
    let ViewForDoneButtonOnKeyboard = UIToolbar()
    ViewForDoneButtonOnKeyboard.sizeToFit()
    let button = UIButton.init(type: .custom)
    button.setImage(UIImage.init(named: "login-logo"), for: UIControlState.normal)
    button.addTarget(self, action:#selector(doneBtnfromKeyboardClicked), for:.touchUpInside)
    button.frame = CGRect.init(x: 0, y: 0, width:UIScreen.main.bounds.width, height: 30) //CGRectMake(0, 0, 30, 30)
    let barButton = UIBarButtonItem.init(customView: button)
    ViewForDoneButtonOnKeyboard.items = [barButton]
    postTextView.inputAccessoryView = ViewForDoneButtonOnKeyboard
}


func doneBtnfromKeyboardClicked (){
        self.contentView.endEditing(true)
    }

1
投票

您必须使用文本字段的inputAccessoryView。

你可以将下面的代码片段放在viewDidLoad()中:

 override func viewDidLoad() {
    super.viewDidLoad()
      let yourButton = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 60))
      yourButton.backgroundColor = UIColor.blue
      yourButton.setTitle("NEXT", for: .normal)
      yourButton.setTitleColor(UIColor.white, for: .normal)
      yourButton.addTarget(self, action: #selector(self. yourButton), for: .touchUpInside)
      NumtextField.inputAccessoryView = yourButton

 }

 @objc func nextButton()
 {
      print("do something")
 }

0
投票

要添加一个带有完成按钮的工具栏,该按钮可以解除UITextField上方的键盘,您可以使用以下函数编写UITextField扩展:

public func addAccessoryView() {
    let doneButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "resignFirstResponder")
    let flexSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
    let toolbar = UIToolbar()
    toolbar.barStyle = UIBarStyle.Default
    toolbar.translucent = true
    toolbar.tintColor = Color.blue
    toolbar.sizeToFit()
    toolbar.setItems([flexSpace, doneButton], animated: false)
    toolbar.userInteractionEnabled = true
    self.inputAccessoryView = toolbar
}

然后,您可以在文本字段中调用此函数,如下所示:

textfield.addAccessoryView()
© www.soinside.com 2019 - 2024. All rights reserved.