【问题标题】:How to add buttons above keyboard如何在键盘上方添加按钮
【发布时间】:2017-01-04 09:49:50
【问题描述】:

如何在 Stack Exchange 应用程序中像这样在键盘上方添加按钮?而当你长按UITextView中的文字时如何添加“全选”和“全选”?

【问题讨论】:

  • InputAccessoryView 是要走的路
  • 这是两个独立的问题。另外,请向我们展示您迄今为止所做的尝试。
  • @dirtydanee 我不知道该怎么做。我需要其中一种方法
  • 检查this,尝试实施,告诉我们进度!
  • @dirtydanee,你问的是两个不同的问题,好吗?

标签: ios button swift2 swift3 ios10


【解决方案1】:

第一个问题,你可以设置textFieldinputAccessoryView为你的自定义view,这样可以自定义键盘的header。

结果:

你可以像下面这样;

首先,你应该实例化你想在键盘上方添加的视图。

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

【讨论】:

  • 当我添加一个 XIB 文件时,我要实现哪个类,因为 UIView 不允许我创建 XIB 文件?
  • 它工作了:D 如何将该按钮与另一个 ViewController 中的 UITextView 连接,所以当在我的 TextView 中点击该按钮时,所选文本应该是粗体?
【解决方案2】:

我建议为您的UITextField 的附件视图属性创建一个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()
    }
}

这应该是你的输出:

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      你可以把下面的代码 sn-p 放在你的 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")
       }
      

      【讨论】:

        【解决方案5】:

        要添加一个带有完成按钮的工具栏,该按钮会在 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()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-20
          • 2012-04-22
          • 1970-01-01
          • 2016-03-19
          • 2017-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多