我在 MACOS 上使用 SwiftUI
如果我这样做:
Button(action: { } ) {
Text("Press")
.padding()
.background(Color.blue)
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
两个灰色区域是可点击按钮的末端。但我希望按钮是蓝色区域的形状。任何想法如何让整个蓝色区域都可以点击。
(我确实考虑过使用 .onTapGesture 但这不会使按钮动画化,因此您知道您已经点击了它。)
好像应该很简单。
Button(action: {
}){
ZStack{
Circle()
.frame(width: 100, height: 100)
.foregroundColor(.blue)
Text("Press me")
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我:
我只能点击矩形部分。如果您能指出圆圈被切断的原因,也可以加分
编辑:原来这是 macOS 的问题。
编辑 2:正如 Asmari 在下面提到的,您可以使用 PlainButtonStyle:
var body: some View {
VStack{
Button(action: {
print("Pressed!")
}){
Text("Press me")
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}.buttonStyle(PlainButtonStyle())
}.frame(width: 300, height: 500)
}
}
Run Code Online (Sandbox Code Playgroud)
或使用自定义样式:
struct BlueButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}
}
struct ContentView: View …Run Code Online (Sandbox Code Playgroud) 我想创建一个游戏菜单,显示不同选项的列表,如“新游戏”。
我已经意识到使用按钮:
var body: some View {
VStack {
Image("Title")
Group {
Button(LocalizedStringKey("new"), action: {}
Button(LocalizedStringKey("load"), action: {})
Button(LocalizedStringKey("save"), action: {})
Button(LocalizedStringKey("resume"), action: {})
Button(LocalizedStringKey("main"), action: {})
Button(LocalizedStringKey("tutorial"), action: {})
Button(LocalizedStringKey("credits"), action: {})
}
.background(Color.clear)
.font(.custom("Snell Roundhand", size: 24))
.padding()
}
}
Run Code Online (Sandbox Code Playgroud)
如何隐藏按钮的背景矩形?我只想看文字。触摸文本应触发操作。