gpt4 book ai didi

SwiftUI .focused() View 修饰符不工作 .onAppear

转载 作者:行者123 更新时间:2023-12-05 02:41:02 25 4
gpt4 key购买 nike

我正在尝试让新的 iOS 15.0 .focused() View Modifier 在 SwiftUI View 出现时执行:

import SwiftUI

struct MainView: View {
@State var subViewIsDisplayed = false
var body: some View {
VStack {
Spacer()
Button(action: { subViewIsDisplayed.toggle() }) {
Text("Trigger SubView")
}
Spacer()
}
.fullScreenCover(isPresented: $subViewIsDisplayed) {
SubView()
}
}
}

struct SubView: View {
@Environment(\.dismiss) var dismiss
@FocusState private var isFocused: Bool
@State var text = ""

var body: some View {
VStack {
HStack {
Spacer()
Button(action: { dismiss() }) {
Image(systemName: "xmark")
}
}
Spacer()
TextField("Please autofocus :(", text: $text)
.focused($isFocused)
Spacer()
}
.onAppear {
self.isFocused = true
}
}
}

struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}

在我的物理设备上,触发“SubView”不会调出键盘(正如我对 .focused 所期望的那样)。我做错了什么?

最佳答案

        .onAppear {
DispatchQueue.main.async {
isFocused = true
}
}

DispatchQueue.main.async,一如既往的成功

enter image description here

关于SwiftUI .focused() View 修饰符不工作 .onAppear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68307930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com