OnkarK
OnkarK

Reputation: 3793

UITextField inputView is not working in Swift

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.

Upvotes: 2

Views: 11212

Answers (5)

bjcullinan
bjcullinan

Reputation: 71

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

Upvotes: 1

mnl
mnl

Reputation: 481

  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)

Upvotes: 0

MattSenter
MattSenter

Reputation: 3120

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

Upvotes: 3

Keenle
Keenle

Reputation: 12220

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

Upvotes: 4

Ilia
Ilia

Reputation: 1444

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!")
}

Upvotes: 2

Related Questions