13
10

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.

SwiftUIでキーボードを下げる(非表示にする)方法

Last updated at Posted at 2019-10-03

まず、下記の拡張を実装しておきます。
新しいファイル作って保存しておくと良いと思います。

UIApplication+ext.swift
extension UIApplication {
    func endEditing() {
        sendAction(
            #selector(UIResponder.resignFirstResponder),
            to: nil,
            from: nil,
            for: nil
        )
    }
}

あとは、キーボードを下げたいところで呼び出して使うだけです。

ContentView.swift
struct ContentView : View {
    var body: some View {
        Button(action: {
            // ここでキーボードを下げる
            UIApplication.shared.endEditing()
        }) {
            Image(systemName: "keyboard.chevron.compact.down")
        }
    }
}

SFアイコンを使ってキーボードを下げるボタンを実装してみた例になります。

参考

13
10
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
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?