UITextField inputView is not working in Swift

OnkarK picture OnkarK · Aug 13, 2014 · Viewed 9.8k times · Source

I am trying to set inputView & inputAccessoryView for UITextField. But, as I start tap on textField only inputAccessoryView is come from bottom.

Following is my code:

@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var toolBar: UIToolbar!

override func viewDidLoad() {
    super.viewDidLoad()
    self.textField.inputView = self.pickerView;
    self.textField.inputAccessoryView = self.toolBar;
}

Thanks in advance.

Answer

Keenle picture Keenle · Aug 13, 2014

I was really surprised when saw such a bug! You code looks 100% valid and must work. I even created small project to reproduce the problem. And succeeded with reproduction.

So my steps were:

  1. Creare simple project with UITextView in Xib that gets instantiated in application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!). Run and see that default keyboard gets displayed.

  2. Add two more views to the Xib and connect them to MyViewController. And set inputView and accessoryInputView in viewDidLoad as you did.

  3. Run and see the problem.

Answer starts here:

Then I've just reset emulator, cleaned the build folder and run the code again. And it worked as expected!

It looks for me like a bug with either xCode or emulator(or may be both) but I cannot say who is guilty an why.

Screenshot (view with buttons is my keyboard :)

enter image description here