Swiftui如何停止按钮在其框架外触发?

发布于 2025-02-10 21:16:13 字数 1924 浏览 2 评论 0原文

我试图在单击边框外时停止以下按钮触发,并且在框架内部单击时仅触发操作。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜味拾荒者 2025-02-17 21:16:13

我遇到了这个问题,并且能够使用.clipped()。buttonstyle(borderlessbuttonStyle())进行修复。

Button(action: {
    //action
}, label: {
    Text("Click").clipped().buttonStyle(BorderlessButtonStyle())
}).clipped().buttonStyle(BorderlessButtonStyle()) 

I had this issue and was able to fix it with .clipped().buttonStyle(BorderlessButtonStyle())

Button(action: {
    //action
}, label: {
    Text("Click").clipped().buttonStyle(BorderlessButtonStyle())
}).clipped().buttonStyle(BorderlessButtonStyle()) 
高跟鞋的旋律 2025-02-17 21:16:13

单击框架内部时仅触发动作

按钮中的文本上的最后一个修饰符为.frame(maxWidth:.infinity,Alignment:.center:.center)

因此,由于您将maxWidth设置为.infinity,因此该按钮具有无限的框架。这意味着动作仅在单击框架内部时才会触发,但是您将框架设置为无限,因此单击 ewhere 会触发动作。

因为您制作了框架无限,所以您也可以使动作区域无限,这就是为什么单击“任何地方”会触发您的按钮的原因。

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

So, because you set maxWidth to .infinity, the button has an infinite frame. 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.

执妄 2025-02-17 21:16:13

我想你想要这个

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

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 !!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文