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

Sha*_*eba 9 macos swift swiftui

好像应该很简单。

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

这给了我:

预览

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

编辑:原来这是 macOS 的问题。

MacOS 上 SwiftUI 中的按钮问题

编辑 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 {
    var body: some View {
        VStack{
            Button(action: {
                print("Pressed!")
            }){
               Text("Press me")

            }.buttonStyle(BlueButtonStyle())
        }.frame(width: 300, height: 500)

    }
}
Run Code Online (Sandbox Code Playgroud)

小智 14

试试这个:

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

输出将是:

在此处输入图片说明


use*_*734 3

为什么圆被切掉了??

当您将覆盖应用到视图时,原始视图将继续为结果视图提供布局特征。

不幸的是,即使对于 .background() 修饰符也是如此!

您需要更改按钮的框架,尤其是在 Mac 上,默认配置下按钮的背景是可见的。

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button(action: {
            print("tap")
        }) {
            Text("Button").font(.largeTitle)
        }.buttonStyle(BlueCircleButtonStyle())
        // to see resulting layout bounds
        .border(Color.red)
    }
}

struct BlueCircleButtonStyle: ButtonStyle {
    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label.padding().modifier(MakeSquareBounds()).background(Circle().fill(Color.blue))

    }
}

struct MakeSquareBounds: ViewModifier {

    @State var size: CGFloat = 1000
    func body(content: Content) -> some View {
        let c = ZStack {
            content.alignmentGuide(HorizontalAlignment.center) { (vd) -> CGFloat in
                DispatchQueue.main.async {
                    self.size = max(vd.height, vd.width)
                }
                return vd[HorizontalAlignment.center]
            }
        }
        return c.frame(width: size, height: size)
    }
}
Run Code Online (Sandbox Code Playgroud)

mac上运行结果 在此输入图像描述

点击蓝色即可执行操作...

有一种方法可以在按下时设置不同的样式(检查 ButtonStyle 属性)