swift - SwiftUI 按钮大小/形状/颜色不变
问题描述
我正在尝试使用 SwiftUI 为 MacOS 应用程序制作自定义按钮,但是,我似乎无法调整按钮区域的大小,并且按钮的颜色似乎无法更改。我在网上找到的所有示例都说使用 .frame 或 .padding 来设置宽度和高度,但这不会改变按钮的形状。有没有其他人在开发 MacOS 应用程序时遇到过这种情况?此处的按钮图片
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)
}
}
解决方案
您必须使用.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())
推荐阅读
- bpf - BPF crc32 奇怪的错误:最后一个 insn 不是退出或跳转
- node.js - Nodejs API 在 localhost 中回复 JSON,但相同的 API 调用,在部署 heroku 时回复空 JSON
- firebase-dynamic-links - Firebase 动态链接可以用于通用应用邀请和特定页面邀请以跟踪推荐吗?
- c# - 使用范围运算符给出错误预定义类型“System.range”未定义或导入
- ethereum - 使用数组中数百个预填充结构创建 Solidity 合约的 Gas 问题
- python - 如何导出 csv 或文本文件中的所有链接?
- c# - 将项目转换为 NETSDK 格式后表单中的资源异常
- python-3.x - 多台计算机通信 - multiprocessing.Listener,确保端口保持可用?
- spring - 在 @SpringBootTest 中自动装配时,WebTestClient 为空
- roslyn - Roslyn - 获取对分配给属性的字符串的引用