Swiftui如何停止按钮在其框架外触发?
我试图在单击边框外时停止以下按钮触发,并且在框架内部单击时仅触发操作。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这个问题,并且能够使用
.clipped()。buttonstyle(borderlessbuttonStyle())
进行修复。I had this issue and was able to fix it with
.clipped().buttonStyle(BorderlessButtonStyle())
按钮中的文本上的最后一个修饰符为
.frame(maxWidth:.infinity,Alignment:.center:.center)
。因此,由于您将
maxWidth
设置为.infinity
,因此该按钮具有无限的框架。这意味着动作是仅在单击框架内部时才会触发,但是您将框架设置为无限,因此单击 ewhere 会触发动作。因为您制作了框架无限,所以您也可以使动作区域无限,这就是为什么单击“任何地方”会触发您的按钮的原因。
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.
我想你想要这个
I assume you wanted this