iosfunctionbuttonswiftuiaction

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)
                                
                 
                            })

Solution

  • 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 !!