我想在彈出視窗出現時用鍵盤激活文本欄位,就像用戶點擊一樣,但是在常規視圖中作業時,以下代碼在彈出視窗中出現時不起作用。
有什么解決辦法嗎?謝謝你。
struct ContentView: View {
@State var str = ""
@State var show = false
@FocusState private var focused: Bool
var body: some View {
VStack {
Text("Popover")
.onTapGesture {
show.toggle()
}
.popover(isPresented: $show) {
TextField("Popover Textfield", text: $str)
.focused($focused)
.onAppear {
focused = true
}
}
.frame(width: 100, height: 100)
}
}
}
uj5u.com熱心網友回復:
@FocusState 系結到根視圖,只需像這樣拆分它:
import SwiftUI
struct focusText: View {
@State var str = ""
@State var show = false
var body: some View {
VStack {
Text("Popover")
.onTapGesture {
show.toggle()
}
.popover(isPresented: $show) {
popView(str: $str)
}
}
.frame(width: 100, height: 100)
}
}
struct popView: View {
@Binding var str: String
@FocusState private var focused: Bool
var body: some View {
VStack {
TextField("Popover Textfield", text: $str)
.focused($focused)
}
.onAppear {
focused = true
}
}
}
請記住,默認情況下模擬器不顯示鍵盤。

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/532671.html
標籤:IOS迅速代码迅捷
