0

I am trying to use a calculator as a custom input view to an UITextField in swift.

I have a calculator separately defined in my app as CalculatorViewController.

I need to access the CalculatorViewController in the UITextField I have in my InputViewController.

I defined the following property in the InputViewController to access the calculator view

var textFieldInputView : UIView!{

        let calculatorInputController = self.storyboard?.instantiateViewControllerWithIdentifier("calculatorVC") as! calculatorViewController

        let calculatorInputView = calculatorInputController.view

        return calculatorInputView
}

then added following code in the textField to access the textFieldInputView

    textField.inputView = textFieldInputView

Now when I click the textField, the calculator popup,as per the below, which is not in the position where keyboard appears. Also none of the buttons working.

Much appreciated if some one could advise me how to get this fixed.

screenshot of custom input view

Isuru
  • 30,617
  • 60
  • 187
  • 303
Magesh
  • 277
  • 1
  • 4
  • 13

1 Answers1

3

Resolved this as per the below.

  1. Created a interface builder "keyboard.xib" with all the UIButton and UILabel for display.
  2. Linked the keyboard.xib to calculatorViewController
  3. Subclass the view controller (say DetailViewController) where the UITextFields are present as the subclass of calculatorViewController.
  4. Created a UIView as per the below code

        var customkeyboardView : UIView {
    
        let nib = UINib(nibName: "CalculatorView", bundle: nil)
        let objects = nib.instantiateWithOwner(self, options: nil)
        let cView = objects[0] as! UIView
        return cView
    }
    
  5. Assign the customkeyboardView as an inputView of the UITextField as indicated below

    textField.inputView = customkeyboardView
    
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
Magesh
  • 277
  • 1
  • 4
  • 13