Prevent Keyboard from appearing when tapping on UITextField

前端 未结 6 732
孤城傲影
孤城傲影 2021-01-19 22:11

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 UIText

相关标签:
6条回答
  • 2021-01-19 22:28

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

    textField.inputAssistantItem.leadingBarButtonGroups = []
    textField.inputAssistantItem.trailingBarButtonGroups = []
    
    0 讨论(0)
  • 2021-01-19 22:37

    You can hide keyboard in UITextFieldDelegate method textFieldDidBeginEditing:(textField: UITextField) like below :

    func textFieldDidBeginEditing:(textField: UITextField) {
        self.view.endEditing(true)
    }
    
    0 讨论(0)
  • 2021-01-19 22:40

    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
    
    0 讨论(0)
  • 2021-01-19 22:40

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

    self.txtresult.inputView = UIView()
      self.txtresult.inputAccessoryView = UIView()
    
    0 讨论(0)
  • 2021-01-19 22:44

    you could use an "empty" inputview like this:

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        let inputView = UIView(frame: .zero)
        inputView.backgroundColor = UIColor.clearColor()
        inputView.opaque = false
        textField.inputView = inputView
    
        return true
    }
    
    0 讨论(0)
  • 2021-01-19 22:45

    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
    }
    
    0 讨论(0)
提交回复
热议问题