How to dismiss number pad keyboard by tapping anywhere

后端 未结 15 2160
梦如初夏
梦如初夏 2020-12-04 17:53

I\'d like to know the simplest code to dismiss the number pad keyboard when tapping anywhere outside the number pad. It\'s a simple application to input a number inside a te

15条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 18:19

    You can implement @mbm's answer really nicely by putting a didSet on the outlet and attaching your inputAccessoryView there:

    Swift code:

      @IBOutlet var input: UITextField! { didSet {
        let toolbar = UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 0, height: 44)))
        toolbar.items = [
          UIBarButtonItem(barButtonSystemItem: .done, target: self.input,
            action: #selector(resignFirstResponder))
        ]
        input.inputAccessoryView = toolbar
      }}
    

    In xcode 8 / iOS 10 It ends up looking something like this:

提交回复
热议问题