【发布时间】:2020-04-25 14:05:53
【问题描述】:
看起来应该很简单。
Button(action: {
}){
ZStack{
Circle()
.frame(width: 100, height: 100)
.foregroundColor(.blue)
Text("Press me")
}
}
这给了我:
我只能点击矩形部分。 如果你能指出圆圈被切断的原因,还可以加分
编辑:原来这是 macOS 的问题。
Issue with Buttons in SwiftUI on 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)
}
}
或使用自定义样式:
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 {
var body: some View {
VStack{
Button(action: {
print("Pressed!")
}){
Text("Press me")
}.buttonStyle(BlueButtonStyle())
}.frame(width: 300, height: 500)
}
}
【问题讨论】:
-
您只需要像this post 一样使用
PlainButtonStyle并根据需要自定义按钮。 -
@Asperi,这只是部分正确,尝试检查结果大小......垂直尺寸仍然存在问题。