简体   繁体   English

SwiftUI 如何阻止按钮在其框架之外触发?

[英]SwiftUI How to stop Button from triggering outside of its frame?

I'm trying to stop the following button from triggering when clicking outside of the border and only trigger the action when clicking inside the frame.我试图阻止在边框外单击时触发以下按钮,并且仅在单击框架内时触发动作。

          Button(action: {
                                
                                model.addData(clubID: clubID, clubName: clubName, clubCreator: String(describing: uidx), membersLimit: membersLimit, streamingPlatforms: streamingPlatforms, streamingType: streamingType, teleClubGenres: teleClubGenres, date:  Date.getCurrentDate(), description: description, uid: String(describing: uidx) , currentMembers: [String(describing: uidx)], selectedDate: dateformatter(date: selectedDate), mediaChosen: mediaChosen, streamingPartyLink: streamingPartyLink)
                                isSuccess = true
                                message = "Club made successfully"
                                shown.toggle()

                                clubID = 0
                                clubName = ""
                                clubCreator = ""
                                membersLimit = 0
                                streamingPlatforms = 0
                                streamingType = 0
                                teleClubGenres = []
                                date = ""
                                description = ""
                                uid = ""
                                currentMembers = [String(describing: uidx)]
                                selectedDateTwo = ""
                                mediaChosen = ""
                                streamingPartyLink = "https://www.teleparty.com/"
                            }, label: {
                                Text("Save")
                                    .padding().foregroundColor(Color.black).background(RoundedRectangle(cornerRadius: 10).stroke(lineWidth: 2).background(Color.white.cornerRadius(10))).frame(maxWidth: .infinity, alignment: .center)
                                
                 
                            })

and only trigger the action when clicking inside the frame并且仅在框架内单击时触发动作

The last modifier on the text in your button is .frame(maxWidth: .infinity, alignment: .center) .按钮中文本的最后一个修饰符是.frame(maxWidth: .infinity, alignment: .center)

So, because you set maxWidth to .infinity , the button has an infinite frame.因此,因为您将maxWidth设置为.infinity ,所以该按钮具有无限的框架。 This means that the action IS only being triggered when clicking inside the frame, but you set the frame to be infinite, and so clicking anywhere will trigger the action.这意味着只有在框架内单击时才会触发该动作,但您将框架设置为无限,因此单击任意位置都会触发该动作。

Because you made the frame infinite, you're making the action area infinite, too, which is why clicking "anywhere" will trigger your button.因为您使框架无限,所以您也使动作区域无限,这就是为什么单击“任何地方”会触发您的按钮。

I assume you wanted this我想你想要这个

Button(action: {
    // ... action here
}, label: {
    Text("Save").padding()
        .foregroundColor(Color.black)
        .background(
           RoundedRectangle(cornerRadius: 10).stroke(lineWidth: 2)
              .background(Color.white.cornerRadius(10))
        )
})
.frame(maxWidth: .infinity, alignment: .center)    // << here !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM