3

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

steveSarsawa
  • 1,559
  • 2
  • 14
  • 31
Ben Dev
  • 77
  • 1
  • 12
  • 1
    It’s unclear, what you’re asking? Can you explain a little bit more – Mannopson Jul 29 '19 at 11:40
  • Add `UITextFieldDelegate` in `class ViewController: UIViewController, UITextFieldDelegate` – Sina Jul 29 '19 at 11:55
  • @Mannopson. I am adding toolbar custom view on top of the keyboard. within that keyboard I am adding textview and below some buttons. Here, application home screen I am having add button, whenever I click the add button need to show keyboard with this custom view. how to do that? – Ben Dev Jul 29 '19 at 12:01
  • @Sina I already added that but not working – Ben Dev Jul 29 '19 at 12:02
  • Use this link: https://stackoverflow.com/a/23904935/4116685 – Sina Jul 29 '19 at 12:08
  • @BenDev Fortunately, you can do that by implementing a XIB. It gives you more control, you can assign a button too. – Mannopson Jul 29 '19 at 12:14
  • @Mannopson Please provide some code. – Ben Dev Jul 29 '19 at 12:16

2 Answers2

2
  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
    
vrat2801
  • 538
  • 2
  • 13
1

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.

Mannopson
  • 2,634
  • 1
  • 16
  • 32