在我的代码中,我的顶部有一个搜索栏,在圆圈内有一个“放大镜”作为按钮。单击它时,它会显示一个文本字段,然后必须按它才能打开键盘。我需要在代码中实现什么,以便当我单击“放大镜”图像时,它可以立即打开键盘,而无需再次按键盘?
VStack(spacing: 0) {
HStack{
if !self.show{
Text("Buscar")
.fontWeight(.bold)
.font(.title)
.foregroundColor(.white)
}
Spacer(minLength: 0)
HStack{
if self.show{
Image(systemName: "magnifyingglass")
.padding(.leading, 5)
TextField("Buscar", text: self.$txt)
Button(action: {
withAnimation {
self.txt = ""
self.show.toggle()
}
}) {
Image(systemName: "xmark").foregroundColor(.black)
}
.padding(.horizontal, 8)
}
else{
Button(action: {
withAnimation {
self.show.toggle()
}
}) {
Image(systemName:"magnifyingglass")
.resizable()
.frame(width: 22, height: 22)
.font(Font.system(.body).bold())
.foregroundColor(.black)
.padding(10)
}
}
}
.padding(self.show ? 10 : 0)
.background(Color.white)
.cornerRadius(90)
}
.padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top == 0 ? 0 : UIApplication.shared.windows.first?.safeAreaInsets.top)
.padding(.horizontal)
.padding(.bottom , 10)
.background(Color("Color"))
答案 0 :(得分:0)
编辑:很抱歉,SwiftUI上目前没有成为beginFirstResponder,根据此答案,您可以通过实现自己的相似行为来实现相同的行为。 https://stackoverflow.com/a/56508132/5848719