8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UTextFieldのソフトキーボードを数値のみにして完了ボタンを追加する

Posted at

UITextFieldやUITextViewのkeyboardTypeをnumberPadにした場合に
ソフトキーボードに完了キーが無くて困ったので追加方法を共有します。

let toolbar: UIToolbar = UIToolbar()
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
                            target: nil,
                            action: nil)
let done = UIBarButtonItem(title: "done",
                           style: .done,
                           target: self,
                           action: #selector(doSomething))
toolbar.items = [space, done]
toolbar.sizeToFit()
self.textField.inputAccessoryView = toolbar
self.textField.keyboardType = .numberPad

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?