10
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

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

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

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アイコンを使ってキーボードを下げるボタンを実装してみた例になります。

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
10
Help us understand the problem. What are the problem?