swift2 如何在键盘上方添加按钮

eulz3vhy  于 2022-11-06  发布在  Swift
关注(0)|答案(5)|浏览(251)

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

jjhzyzn0

jjhzyzn01#

第一个问题,你可以把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?

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

import UIKit

class CustomAccessoryView: UIView {

    @IBAction func clickLoveButton(_ sender: UIButton) {

        print("Love button clicked")
    }
}
8tntrjer

8tntrjer2#

我建议为您的UITextField的accessoryView属性创建一个toolbar
我们的想法是在第一次显示文本字段之前添加一次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()
    }
}

您的输出应该是:

klsxnrf1

klsxnrf13#

您必须使用文本字段的inputAccessoryView。
你可以把下面的代码片段放到你的viewDidLoad()中:

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

 }

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

js81xvg64#

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

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)
    }
zqdjd7g9

zqdjd7g95#

要添加一个带有“完成”按钮的工具栏,该按钮可关闭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()

相关问题