swiftui文本文件和按钮内的按钮
当用户单击视图的背景区域时,我试图触发该按钮。 作为解决方案,我决定制作一个按钮,并将视图作为标签放入按钮中。 但问题是,该按钮甚至在文本字段上被触发,有时在内部的另一个按钮上也会被触发 如何在我选择的某些视图(本例中为文本字段和按钮)上禁用“后退”按钮 我将我的代码附在下面和下面swiftui文本文件和按钮内的按钮,swift,swiftui,ios13,xcode11,swift5,Swift,Swiftui,Ios13,Xcode11,Swift5,当用户单击视图的背景区域时,我试图触发该按钮。 作为解决方案,我决定制作一个按钮,并将视图作为标签放入按钮中。 但问题是,该按钮甚至在文本字段上被触发,有时在内部的另一个按钮上也会被触发 如何在我选择的某些视图(本例中为文本字段和按钮)上禁用“后退”按钮 我将我的代码附在下面和下面 导入快捷界面 结构按钮:视图{ @状态变量备注:String=“” var body:一些观点{ 按钮(操作:{ 打印(“单击向下按钮”) }) { VStack(间距:10){ 文本(“之前”) .foregrou
导入快捷界面
结构按钮:视图{
@状态变量备注:String=“”
var body:一些观点{
按钮(操作:{
打印(“单击向下按钮”)
}) {
VStack(间距:10){
文本(“之前”)
.foregroundColor(.白色)
文本字段(“键入备忘录”,文本:$memo)
.font(.custom(“SFProDisplay Regular”,大小:12))
.textFieldStyle(RoundedBorderTextFieldStyle())
文本(“之后”)
.foregroundColor(.白色)
按钮(操作:{
打印(“单击内部按钮”)
}) {
文本(“内部按钮”)
.foregroundColor(.白色)
}
.背景(颜色.红色)
}
}
.背景(颜色.蓝色)
.padding()
}
}
结构按钮非按钮预览:PreviewProvider{
静态var预览:一些视图{
buttonnonbutton()
}
}
如果我正确理解了您的目标,那么最好使用ontapsignate
而不是按钮,如下所示
var body: some View {
VStack(spacing: 10) {
Text("before")
.foregroundColor(.white)
TextField("type memo", text: $memo)
.font(.custom("SFProDisplay-Regular", size: 12))
.textFieldStyle(RoundedBorderTextFieldStyle())
.onTapGesture {} // << just blocks outer tap
Text("after")
.foregroundColor(.white)
Button(action: {
print("inside button clicked")
}) {
Text("inside button")
.foregroundColor(.white)
}
.background(Color.red)
}
.background(Color.blue)
.onTapGesture {
print("background area clicked")
}
.padding()
}
var主体:一些视图{
VStack(间距:10){
文本(“之前”)
.foregroundColor(.白色)
文本字段(“键入备忘录”,文本:$memo)
.font(.custom(“SFProDisplay Regular”,大小:12))
.textFieldStyle(RoundedBorderTextFieldStyle())
.ontapsivert{}/如果我正确理解了您的目标,那么最好使用ontapsivert
而不是按钮,如下所示
var body: some View {
VStack(spacing: 10) {
Text("before")
.foregroundColor(.white)
TextField("type memo", text: $memo)
.font(.custom("SFProDisplay-Regular", size: 12))
.textFieldStyle(RoundedBorderTextFieldStyle())
.onTapGesture {} // << just blocks outer tap
Text("after")
.foregroundColor(.white)
Button(action: {
print("inside button clicked")
}) {
Text("inside button")
.foregroundColor(.white)
}
.background(Color.red)
}
.background(Color.blue)
.onTapGesture {
print("background area clicked")
}
.padding()
}
var主体:一些视图{
VStack(间距:10){
文本(“之前”)
.foregroundColor(.白色)
文本字段(“键入备忘录”,文本:$memo)
.font(.custom(“SFProDisplay Regular”,大小:12))
.textFieldStyle(RoundedBorderTextFieldStyle())
.ONTAPPORATE{}//在textField上添加Tapporter的想法是我想不出来的。我解决了问题。谢谢!在textField上添加Tapporter的想法是我想不出来的。我解决了问题。谢谢!