Create custom input for UITextField in Swift

3.1k views Asked by At

I'm programming a iOS app in Swift language and all I have to do is create a custom input for a textfield.

I created an additional View Controller with two buttons and what I want is this view controller (instead of the keyboard) to pop-up when I highlight my textfield.
Basically what I want is to create a small custom keyboard, but I just want it to be inside my app: I found lots of tutorials about creating custom keyboards, but it is not the same as having a simple View Controller that pops-up when text field is highlighted.

Can you suggest how to assign my view controller to textField.inputViewController in Swift?

Thanks

2

There are 2 answers

0
fishinear On

As far as I know, you cannot use a view controller. You need to make your own view and assign it to the inputView field. Make sure the view has a delegate so it knows which field to use:

MyInputView keyboard = ...
field.inputView = keyboard
keyboard.delegate = field
0
EckhardN On

You can assign your own viewcontroller to inputViewcontroller:

Your viewController has to be a subclass of UIInputViewController for example:

class CustomInputViewController: UIInputViewController {
    @IBOutlet var insertTextButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.inputView?.translatesAutoresizingMaskIntoConstraints = false
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func insertText(_ button: UIButton){
        self.textDocumentProxy.insertText((button.titleLabel?.text)!);
    }
}

Here with only one button insertTextButton which I design in the xib-file.

In your main view controller you need a subclass of your textfield (or textview):

class textfield: UITextField {
    var _inputViewController : UIInputViewController?
    override public var inputViewController: UIInputViewController?{
        get { return _inputViewController }
        set { _inputViewController = newValue }
    }
}

which you assign to your textfield.

Now you can assign your own inputViewcontroller to your textfield, for example:

class ViewController: UIViewController {

    private var customInputViewController = CustomInputViewController(nibName: "CustomInputViewController",
                                                                      bundle: nil)
    @IBOutlet var textField: textfield!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textField.inputViewController = customInputViewController
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I used the xib-file with the name CustomInputViewController.xib to design the keyboard