TextFieldにautofocusしてテキスト入力画面に自動で遷移するようにしたいと考えております。
そこで調べたところ以下のようにするのが良いと書いてありましたが、watchOSではうまく動きません。
なにが原因かヒントを頂くことは可能でしょうか?
swift
1struct ContentView: View { 2 enum FocusTextFields { 3 case writeField 4 } 5 6 @FocusState private var focusedField: FocusTextFields? 7 @State var text: String = "" 8 9 var body: some View { 10 TextField("textField", text: $text) 11 .focused($focusedField, equals: .writeField) 12 .onAppear { 13 DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { 14 self.focusedField = .writeField 15 } 16 } 17 }
追記: 私が実現したい動きとしては、Apple Watchのアプリを開いたらすぐに音声認識が始まって、喋ったことが文字に起こされる…というもの想定しております。それがTextFieldでできるのかは判断が付きませんがとりあえず挑戦しています。わかりづらくてすみません。
これ?
https://swiftwithmajid.com/2020/12/02/focus-management-in-swiftui/#focus-on-watchos-and-tvos
おお…まさにこの情報を求めていました!
しかし、以下のようにしてApple Watchにビルド後、アプリを開いてみてもフォーカスされている様子がありません…。
```
struct ContentView: View {
@State private var writeField = ""
@Namespace private var namespace
@State private var hasFilledCredentials = false
var body: some View {
VStack {
Text("2022-05-03")
TextField("日記を書く", text: $writeField)
.prefersDefaultFocus(!hasFilledCredentials, in: namespace)
}.focusScope(namespace)
}
```
それは残念…。私は watchOS 開発はしてないので、これ以上は分かりません。
ヒントを頂き、ありがとうございました!
あなたの回答
tips
プレビュー