I want to hide the keyboard if the user clicks on a textfield. Does anybody have an idea how to do that in swift 2?
Asked
Active
Viewed 396 times
1
-
1I did't get your idea. You dont want your textfield will be selected by user? – Cong Tran Dec 01 '15 at 08:27
-
It should be selected by the user but i don't want to show the keyboard. Instead of the keyboard i want to show a picker. I know how to do that with a button but i want a textfield so that i can put the value of the picker inside the textfield – Philipp Dec 01 '15 at 08:35
-
2@Philipp a button still might be a better choice, as you can easily change the button title after you select something from the picker. Overriding default behaviour on a text field is not a very good idea, but if you still want to do so, consider using `inputView` property on the text field. – Dominik Hadl Dec 01 '15 at 08:47
-
Possible duplicate of [hide keyboard for text field in swift programming language](http://stackoverflow.com/questions/24908966/hide-keyboard-for-text-field-in-swift-programming-language) – Nimit Parekh Dec 01 '15 at 08:47
-
I don't think it's a duplicate @NimitParekh, Philipp doesn't want the textField to appear, he wants something else to appear instead of the keyboard. – Lukesivi Dec 01 '15 at 09:06
-
I agree with DominikHadl that's the best option. Or you set `UITapGestureRecognizer` for the textfield, and when a tap occurs, you do what you need it to do (show a picker). Let us know if you may need that code instead! @Philipp – Lukesivi Dec 01 '15 at 09:07
2 Answers
4
Make sure to set the text field delegate and return false
in this UITextFieldDelegate method:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
return false
}
This will prevent the keyboard from showing. You can also place the code to open the picker in that method.

andreamazz
- 4,256
- 1
- 20
- 36
0
Showing a text field on screen doesn't mean that only a text field is there. You could have a text field as a sub view, with user interaction disabled. Then, add a transparent button as a sibling view with the same frame (ensuring it's in front). When the button is tapped, which from a user point of view is tapping the text field, you can show your picker.
The text field also offers direct support for displaying a picker view by allowing you to set its inputView.

Wain
- 118,658
- 15
- 128
- 151
-
-
@NicolasMiari That's the direct support way I added, just needed to get the link. The multiple view way can be very effective depending on the UI interaction required. – Wain Dec 01 '15 at 08:45