Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

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

参考

https://stackoverflow.com/questions/56491386/how-to-hide-keyboard-when-using-swiftui

Riscait
FlutterとiOSネイティブのアプリ開発者です。 https://apps.apple.com/jp/developer/id914329387
https://nerco.jp
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