Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Keyboard from appearing when tapping on UITextField

I'm using a UITextField to show results of a calculation but I don't want the keyboard to appear when the user taps on the UITextField.

I'm using a UITextField because I still want the user to be able to Copy and Paste the calculation back into the UITextField, but I don't want the keyboard to show up.

UIKeyboardWillHide only works after the keyboard is displayed.

like image 921
Brewski Avatar asked Aug 08 '17 06:08

Brewski


5 Answers

Swift 4.2, This works for me. put in viewDidLoad()

//It will Hide Keyboard
textField.inputView = UIView()
//It will Hide Keyboard tool bar
textField.inputAccessoryView = UIView()
//It will Hide the cursor
textField.tintColor = .white
like image 85
Awais Jamil Avatar answered Oct 08 '22 01:10

Awais Jamil


Its quite simple to do with UITextField. Use this code in viewDidLoad()

self.txtresult.inputView = UIView()
  self.txtresult.inputAccessoryView = UIView()
like image 34
Moin Shirazi Avatar answered Oct 07 '22 23:10

Moin Shirazi


First set delegate with your UITextField in self class.

You can do with below 2 ways.
1. From storyboard
2. From Code ( You can write at viewDidLoad() )

textField.delegate = self

Then declare protocol UITextFieldDelegate in your class.

Now call delegate method.

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return true
}
like image 44
Shah Nilay Avatar answered Oct 07 '22 23:10

Shah Nilay


For iPads, according to this response of @Awais Jamil, add the following code

textField.inputAssistantItem.leadingBarButtonGroups = []
textField.inputAssistantItem.trailingBarButtonGroups = []
like image 39
Mendy Avatar answered Oct 07 '22 23:10

Mendy


textField.inputView = UIView()

This line of code in your textFieldDidBeginEditing func will do the job.

My func:

func textFieldDidBeginEditing(_ textField: UITextField) {
        keyboardView.activeTextField = textField
        textField.inputView = UIView()
    }
like image 44
Harrish Selvarajah Avatar answered Oct 08 '22 00:10

Harrish Selvarajah