gpt4 book ai didi

swift - 如何在 macOS 中的 SwiftUI 中制作一个简单的圆形按钮?

转载 作者:行者123 更新时间:2023-12-03 09:19:23 27 4
gpt4 key购买 nike

好像应该很简单。

        Button(action: {
}){
ZStack{
Circle()
.frame(width: 100, height: 100)
.foregroundColor(.blue)
Text("Press me")
}
}

这给了我:

Preview

我只能点击矩形部分。
如果您能指出圆圈被切断的原因,也可以加分

编辑:原来这是 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)

}
}

最佳答案

试试这个:

import SwiftUI

struct ContentView: View {
var body: some View {
Button(action: {
print("Round Action")
}) {
Text("Press")
.frame(width: 100, height: 100)
.foregroundColor(Color.black)
.background(Color.red)
.clipShape(Circle())
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

输出将是:

enter image description here

关于swift - 如何在 macOS 中的 SwiftUI 中制作一个简单的圆形按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59639048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com