SwiftUI:.searchable でキーボードのエンターが押された時の処理

キーボードでエンターキーが押された時の処理実装においては .onSubmit { ... } modifier を使いイベント検知できるが、.searchable modifier による検索フィールドに対しては検索キーのタップイベントが捕捉できなかった。

.onSubmit には、実は隠れたパラメタ trigger: SubmitTriggers がある。デフォルトで .text が指定されており、入力補完で省略されると気がつけない。

onSubmit(of:_:) | Apple Developer Documentation

以下のように、trigger: .search と指定することで解決できた。

CustomScreen()
   .searchable(text: $text)
// .onSubmit {
//       ❌ 呼ばれない           
// }
   .onSubmit(of: .search, {
      // 👍 呼ばれる
   }

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny