【问题标题】:SwiftUI Button Size/Shape/Color Not ChangingSwiftUI 按钮大小/形状/颜色不变
【发布时间】:2021-03-02 07:38:59
【问题描述】:

我正在尝试使用 SwiftUI 为 MacOS 应用程序制作自定义按钮,但是,我似乎无法调整按钮区域的大小,并且按钮的颜色似乎无法更改。我在网上找到的所有示例都说使用 .frame 或 .padding 来设置宽度和高度,但这不会改变按钮的形状。有没有其他人在开发 MacOS 应用程序时遇到过这种情况? Image of button here

struct customButton: View {
    var body: some View {

        Button(action: {
            print("Delete button tapped!")
        }) {
            HStack {
                Image(systemName: "trash")
                    .resizable()
                    .scaledToFit()
                    .imageScale(.large)
                Text("Delete")
            }
            .padding()
            .background(
                Capsule().strokeBorder(Color.white, lineWidth: 1.25)
            )
        }
        .accentColor(Color.white)
        
    }
}

【问题讨论】:

标签: swift macos swiftui


【解决方案1】:

您必须使用 .buttonStyle(PlainButtonStyle()) 来摆脱默认按钮 UI。

Button(action: {
    print("Delete button tapped!")
}) {
    HStack {
        Image(systemName: "trash")
            .resizable()
            .scaledToFit()
            .imageScale(.large)
        Text("Delete")
    }
    .padding()
    .background(
        Capsule().strokeBorder(Color.white, lineWidth: 1.25)
    )
}
.accentColor(Color.white)
.buttonStyle(PlainButtonStyle())

【讨论】:

  • 非常感谢!我一直在努力寻找解决文档的方法。这个问题已经困扰了我好几个星期了
猜你喜欢
  • 1970-01-01
  • 2015-11-23
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
相关资源
最近更新 更多