2

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
OnkarK
  • 3,745
  • 2
  • 26
  • 33
  • Check if `pickerView` frame is properly set. If it is zero width and height that may cause invisibility. – Keenle Aug 13 '14 at 09:08
  • @Keenle pickerView is connected in Xib. I checked frame of pickerView, its not Zero. – OnkarK Aug 13 '14 at 09:12

5 Answers5

4

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

Keenle
  • 12,010
  • 3
  • 37
  • 46
  • 1
    Can confirm. Reproduced. I thought I was going crazy. Code was completely correct and suddenly UIPickerView showed only the inputAccessoryView. I'm so glad I found this to validate my sanity. – MattSenter Sep 19 '14 at 01:40
3

So the answer is much simpler, turns out. If you use your hardware keyboard instead of the soft keyboard, the simulator switches on its "Connect Hardware Keyboard" setting, and you will no longer get the soft keyboard. The reason the proposed reset of your simulator works is because it also resets this setting. If you don't want to do a full reset, just turn off the hardware keyboard by going to:

Hardware -> Keyboard -> and make sure "Connect Hardware Keyboard" is UNchecked

How to disable hardware keyboard for simulator

MattSenter
  • 3,060
  • 18
  • 18
2

You can create that views programmatically.

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

override func viewDidLoad() {
    super.viewDidLoad()
    var picker = UIPickerView()
    picker.delegate = self
    picker.dataSource = self
    self.pickerView = picker
    self.textField.inputView = pickerView

    var toolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 44))

    var item = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done,
        target: self, action: "doneAction")

    toolbar.setItems([item], animated: true)
    self.textField.inputAccessoryView = toolbar
    self.toolBar = toolbar
}

func doneAction() {
    self.textField.resignFirstResponder()
    println("done!")
}
Ilia
  • 1,434
  • 15
  • 22
1

This worked for me because setting inputView added the view to storyboard but none of the appearance settings worked and viewDidLoad() was not getting called.

self.inputText?.inputViewController?.view.addSubview(self.basicKeyboard) self.inputText!.inputView = self.basicKeyboard self.inputText!.inputAccessoryView = nil

bjcullinan
  • 71
  • 1
  • 3
0
  1. Make sure you override canBecomeFirstResponder in your view controller and return true. (Default is false)
  2. Override the inputAccessoryView property in your view controller and return the view of your choice. ATTENTION: Make sure you remove it from its superview before you return it. (But it can be a view created in IB and linked via outlet)
mnl
  • 411
  • 5
  • 16