我正在try 创建一个带有省略号SFSymbol的圆形按钮,这样椭圆就是白色的,背景是圆形的.我try 了两种方法,一种是填充椭圆图像,另一种是规则椭圆图像.

我希望实现的输出是:

enter image description here

与我目前所拥有的相比:

enter image description here

对于第一个按钮,它是全白的(似乎不能像我想象的那样应用色调加前景样式),而使用第二种方法,它不再是圆形的(除非我可能应用了一个剪裁形状?但这似乎太复杂了).有没有办法达到Button%的yields

struct ContentView: View {
    
    var body: some View {
        VStack(spacing: 50) {
            Button(action: {}, label: {
                Image(systemName: "ellipsis.circle.fill")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 40)
            })
            .tint(.secondary)
            .foregroundStyle(.white)
            
            Button(action: {}, label: {
                Image(systemName: "ellipsis")
                    .resizable()
                    .scaledToFit()
                    .frame(width: 40, height: 40)
            })
            .buttonStyle(.borderedProminent)
            .buttonBorderShape(.capsule)
            .tint(.secondary)
            .foregroundStyle(.white)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background {
            Color.black
                .ignoresSafeArea(edges: .all)
        }
    }
}

推荐答案

如果你想让边界是一个圆,做.buttonBorderShape(.circle),而不是.buttonBorderShape(.capsule).

为了更好地匹配您想要的结果,您可以将图像的frame变小,但增加一些额外的padding.例如,

 Button(action: {}, label: {
    Image(systemName: "ellipsis")
        .resizable()
        .scaledToFit()
        .frame(width: 25, height: 25)
        .padding(7.5)
})
.buttonStyle(.borderedProminent)
.buttonBorderShape(.circle)
.tint(.secondary)
.foregroundStyle(.white)

这会产生以下结果:

enter image description here

如果您需要支持较旧版本的iOS,并且没有.circle版本,您可以使用无边框按钮样式,并在图片中添加圆形背景:

Button(action: {}, label: {
    Image(systemName: "ellipsis")
        .resizable()
        .scaledToFit()
        .frame(width: 25, height: 25)
        .padding(7.5)
        .background(.secondary.opacity(0.5), in: Circle())
})
.buttonStyle(.plain)
.tint(.secondary)
.foregroundStyle(.white)

Ios相关问答推荐

如何加密字符串使用AES. GCM在Dart和解密相同的Swift?

我想在expo 中使用useSafeAreaInsets来获取paddingbottom,但我得到了一个错误

在Android和iOS上从后台恢复应用程序后,inappwebview上出现白屏?

如何在SwiftUI中以一定Angular 绘制直线(&A)

当操作修改查看条件时,按钮不重复

动画文本位置

在 Swift 中从 Objective C 集合调用块

如何解码没有名称的 JSON 数组?

SwiftUI 我自己的渲染函数的返回类型是什么?

Apple Push Service 证书不受信任

如何使用 Swift 文档注释

实现一个将块用作回调的方法

setNeedsLayout 和 setNeedsDisplay

如何使用 swift 显示 .svg 图像

动画 UILabel 字体大小更改

Xcode 4.1 致命错误:自构建预编译头文件后修改了 stdlib

如何从一个特定的视图控制器中隐藏导航栏

如何从 UIPickerView 获取选定的值

UISegmentedControl 以编程方式更改段数

Xcode 6 进程启动失败:try 启动应用程序超时