如何在macOS SwiftUI中启用渐变加减按钮
在SwiftUI中实现渐变加减按钮[+|–]
可以在SwiftUI中实现这类渐变加减按钮,通过自定义视图组合和按钮样式配置,就能复刻出符合macOS设计规范的渐变加减按钮效果。

更新
我基于已有回答做了外观调整,现在的渐变按钮样式与macOS 13设置应用中的样式一致,欢迎提出改进建议。

import SwiftUI struct TestView: View { @State private var selection: Int? struct GradientButton: View { var glyph: String var body: some View { ZStack { Image(systemName: glyph) .fontWeight(.medium) Color.clear .frame(width: 24, height: 24) } } } var body: some View { Form { Section { List(selection: $selection) { ForEach(0 ..< 5) { Text("Item \($0)") } } .padding(.bottom, 24) .overlay(alignment: .bottom, content: { VStack(alignment: .leading, spacing: 0) { Divider() HStack(spacing: 0) { Button(action: {}) { GradientButton(glyph: "plus") } Divider().frame(height: 16) Button(action: {}) { GradientButton(glyph: "minus") } .disabled(selection == nil ? true : false) } .buttonStyle(.borderless) } .background(Rectangle().opacity(0.04)) }) } } .formStyle(.grouped) } }
内容的提问来源于stack exchange,提问作者Roman Banks
相关产品推荐
相关产品推荐

