简体   繁体   中英

How to enable Gradient Buttons (plus and minus) on macOS SwiftUI

Is there a way to enable Gradient Buttons [ + | – ] with SwiftUI? Can't find any useful information about this topic.

https://developer.apple.com/design/human-interface-guidelines/components/menus-and-actions/buttons#gradient-buttons/

在此处输入图像描述


Update

I made a few cosmetic changes to @workingdogsupportUkraine answer. Now Gradient Buttons looks similar as in the Settings app on macOS 13. Please feel free to suggest any enhancements

在此处输入图像描述

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)
    }
}

you could try something simple like this:

struct ContentView: View {
    var body: some View {
        List {
            ForEach(0 ..< 5) { Text("item \($0)") }
            HStack {
                Button(action: {print("plus")}) {
                    Image(systemName: "plus").imageScale(.small)
                }
                Divider()
                Button(action: {print("minus")}) {
                    Image(systemName: "minus").imageScale(.small)
                }
            }.buttonStyle(.borderless)
             .frame(width: 66, height: 33)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM