Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SwiftUI按钮仅点击文本部分_Swiftui - Fatal编程技术网

SwiftUI按钮仅点击文本部分

SwiftUI按钮仅点击文本部分,swiftui,Swiftui,“我的按钮”的背景区域未检测到用户交互。与所述按钮交互的唯一方法是点击按钮的文本/标签区域。如何使整个按钮可点击 struct ScheduleEditorButtonSwiftUIView: View { @Binding var buttonTagForAction : ScheduleButtonType @Binding var buttonTitle : String @Binding var buttonBackgroundColor :

“我的按钮”的背景区域未检测到用户交互。与所述按钮交互的唯一方法是点击按钮的文本/标签区域。如何使整个按钮可点击

struct ScheduleEditorButtonSwiftUIView: View {

    @Binding  var buttonTagForAction : ScheduleButtonType
    @Binding  var buttonTitle        : String
    @Binding  var buttonBackgroundColor : Color


    let buttonCornerRadius = CGFloat(12)

    var body: some View {

        Button(buttonTitle) {
              buttonActionForTag(self.buttonTagForAction)
        }.frame(minWidth: (UIScreen.main.bounds.size.width / 2) - 25, maxWidth: .infinity, minHeight: 44)

                            .buttonStyle(DefaultButtonStyle())
                            .lineLimit(2)
                            .multilineTextAlignment(.center)
                            .font(Font.subheadline.weight(.bold))
                            .foregroundColor(Color.white)

                            .border(Color("AppHighlightedColour"), width: 2)
                           .background(buttonBackgroundColor).opacity(0.8)

                            .tag(self.buttonTagForAction)
                            .padding([.leading,.trailing], 5)
       .cornerRadius(buttonCornerRadius)

    }
}

这解决了我这边的问题:

var body: some View {
    GeometryReader { geometry in
        Button(action: {
            // Action
        }) {
            Text("Button Title")
                .frame(
                    minWidth: (geometry.size.width / 2) - 25,
                    maxWidth: .infinity, minHeight: 44
                )
                .font(Font.subheadline.weight(.bold))
                .background(Color.yellow).opacity(0.8)
                .foregroundColor(Color.white)
                .cornerRadius(12)

        }
        .lineLimit(2)
        .multilineTextAlignment(.center)
        .padding([.leading,.trailing], 5)
    }
}


您使用
UIScreen
而不是
GeometryReader
有什么原因吗?

我认为这是一个更好的解决方案,将
.frame
值添加到
文本()
中,按钮将覆盖整个区域,但我发现了两种方法-

选项1:使用几何体读取器 选项2:使用带垫片的HStack
我在这里的思考过程是,尽管选项1更简洁,但我还是会选择选项2,因为它与父级的大小(通过
GeometryReader
)耦合较少,更符合我认为SwiftUI应该如何使用
HStack
VStack
,等等。

正确的解决方案是使用
.contentShape()
API

按钮(操作:操作){
HStack{
垫片()
文本(“我的按钮”)
垫片()
}
}
.contentShape(矩形())

您可以更改提供的形状以匹配按钮的形状;如果您的按钮是一个
RoundedRectangle
,您可以提供它。

您可以通过添加修改器来定义命中测试的内容形状:

重要的是你必须在按钮的内容里面应用

Button(action: {}) {
    Text("Select file")
       .frame(width: 300)
       .padding(100.0)
       .foregroundColor(Color.black)
       .contentShape(Rectangle()) // Add this line
}
.background(Color.green)
.cornerRadius(4)
.buttonStyle(PlainButtonStyle())
另一个

Button(action: {}) {
    VStack {
       Text("Select file")
           .frame(width: 100)
       Text("Select file")
           .frame(width: 200)
    }
    .contentShape(Rectangle())
}
.background(Color.green)
.cornerRadius(4)
.buttonStyle(PlainButtonStyle())

这就是你要找的。我会给出一个答案,但这是最好的解释:非常感谢您的工作添加了一些
填充
,并使用
contentShape
不知道GeometryReader。谢谢你提供的信息,我会仔细阅读的。回答得好。我对按钮本身使用了大小修改器,而不是文本:)这是一个很好的修复,在Xcode 11.5 beta版上不起作用。我尝试了两种方法,一种是答案中的方法,另一种是将大小移动到自定义按钮样式,这在Xcode 12.5中非常有效,谢谢!如何使用周围的间隔效果,即整个按钮对点击事件敏感,而不仅仅是文本内容?对我来说,第二个选项更好。是的,这是真正的答案,无论您是否有背景色。Adding.contentShape(Rectangle())这是唯一对我有效的解决方案,除了我必须把它放在按钮内部的堆栈上,而不是按钮本身。
Button(action: {}) {
    Text("Select file")
       .frame(width: 300)
       .padding(100.0)
       .foregroundColor(Color.black)
       .contentShape(Rectangle()) // Add this line
}
.background(Color.green)
.cornerRadius(4)
.buttonStyle(PlainButtonStyle())
Button(action: {}) {
    VStack {
       Text("Select file")
           .frame(width: 100)
       Text("Select file")
           .frame(width: 200)
    }
    .contentShape(Rectangle())
}
.background(Color.green)
.cornerRadius(4)
.buttonStyle(PlainButtonStyle())