Reputation: 3793
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
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
Reputation: 481
canBecomeFirstResponder
in your view
controller and return true. (Default is false)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
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
Upvotes: 3
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:
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.
Add two more views to the Xib and connect them to MyViewController. And set inputView
and accessoryInputView
in viewDidLoad
as you did.
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 :)
Upvotes: 4
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