1

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?

6
  • 1
    I did't get your idea. You dont want your textfield will be selected by user? Commented Dec 1, 2015 at 8: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 Commented Dec 1, 2015 at 8: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. Commented Dec 1, 2015 at 8:47
  • Possible duplicate of hide keyboard for text field in swift programming language Commented Dec 1, 2015 at 8: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. Commented Dec 1, 2015 at 9:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, definitely.This keeps it simpler.
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.

2 Comments

This is not very elegant. There has to be a better way.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.