Ben Dev
Ben Dev

Reputation: 77

Swift button click to show keyboard with toolbar view

My scenario, I am trying to implement keyboard with toolbar view using Swift. Here, I need to do whenever I click the button need to show keyboard with toolbar view view without using textview or textfield.

Below My code I am using

Simply I drag UIView to the top of the bar of current ViewController

I declared below code for IBOutlet

@IBOutlet private var toolbarView: UIView!
@IBOutlet private var textView: UITextView!

In ViewDidload I used below code

override func viewDidLoad() {
        super.viewDidLoad()

        textView.inputAccessoryView = toolbarView
    }

In Button action now I am calling like below

@IBAction func CircleClick(_ sender: Any) {
        self.textView.becomeFirstResponder()
    }

Image

Upvotes: 3

Views: 5621

Answers (2)

vrat2801
vrat2801

Reputation: 548

  1. You need to define an outlet for textfield for referencing it.

    @IBOutlet weak var textfield:UITextField!
    
  2. You can activate any text field by calling becomeFirstResponder() on the UITextfield object.

    textfield.becomeFirstResponder()
    
  3. You can call this function in the IBAction of any action of UIButton.

    @IBAction func btnShowKeyboardClicked(){ 
    
        textfield.becomeFirstResponder()
    }
    
  4. For maintaining the custom toolbar as the accessory view, you need to set it to the text field object. First either create the custom toolbar view from code or load using XIB, and assign it to the inputAccessoryView of textfield.

    let customInputAccessoryView =  CustomView() // Load from XIB or from Code
    
    textfield.inputAccessoryView = customInputAccessoryView
    

Upvotes: 2

Mannopson
Mannopson

Reputation: 2684

I would like demonstrate it from scratch, so you can learn from this guide. First off all you should create a view, simply click Command+N button or choose File-New-File from the menu of Xcode. Choose a View template under User Interface section as shown below. Give it a name, in this case a Header (you can name it whatever you want).

enter image description here

And create a class to it, again click Command+N or from menu as I mentioned above. In this case choose a Cocoa Touch Class under Source section as shown below. And name it, as a HeaderView. Subclass of UIView

enter image description here

Open up your Header.xib file from the Project Manage. Click your view, give it a size as a freeform, Top bar to none, and Bottom bar to none. Add a UIButton to this view, I guess you know already, how to do it. And click this view, go to the Identity Inspector. And give it a class, in this a HeaderView.swift is our class.

enter image description here

Connect your button as shown below and we are done: enter image description here

Now all of our focus in code, insert following line of code to the viewDidLoad().

override func viewDidLoad() {
    super.viewDidLoad()

    // Load the view using bundle.
    // Make sure a nib name should be correct
    // And cast it to the class, something like this
    if let headerView = Bundle.main.loadNibNamed("Header", owner: self, options: nil)?.first as? HeaderView {

        // Do some stuff, configuration of the view
        headerView.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.width, height: 44)
        headerView.button.setTitle("Done", for: .normal)
        headerView.button.addTarget(self, action: #selector(self.doneAction(sender:)), for: .touchUpInside)

        // Add this view as an accessory to the text field or text view, in this case I have added this to the text field
        self.textField.inputAccessoryView = headerView
    }

}

Create a custom function to the done button:

@objc func doneAction(sender: UIButton) {
    // Do something
    // Resing your text field or text view
    self.textField.resignFirstResponder()
}

Congrats! You're done. I hope it will help.

Upvotes: 1

Related Questions