swift - 如何在 WatchOS 中使用背景视图填充整个 Button 区域?
问题描述
我试图让我的背景视图跨越 Apple Watch 外形尺寸上按钮的整个区域。
struct SurveyView: View {
var body: some View {
Button(action: {
print("Test tapped.")
}) {
HStack {
Text("Test")
.fontWeight(.semibold)
}
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.foregroundColor(.white)
.background(LinearGradient(gradient: Gradient(colors: [.green, .blue]), startPoint: .leading, endPoint: .trailing))
}
}
}
如何使背景跨越整个 Button?
更新:原来我的问题也与在列表视图中有关。设置蒙版并不能完全解决它。
struct SurveyView: View {
var body: some View {
List() {
Button(action: {
print("Test tapped.")
}) {
Text("Test")
.fontWeight(.semibold)
}
.background(Color.orange)
.mask(RoundedRectangle(cornerRadius: 24))
}
}
}
解决方案
我会通过以下方式做到这一点:
Button(action: {
print("Test tapped.")
}) {
Text("Test")
.fontWeight(.semibold)
.padding()
.foregroundColor(.white)
}
.background(LinearGradient(gradient: Gradient(colors: [.green, .blue]), startPoint: .leading, endPoint: .trailing))
.mask(RoundedRectangle(cornerRadius: 24))
更新:列表的情况
var body: some View {
List() {
Button(action: {
print("Test tapped.")
}) {
Text("Test")
.fontWeight(.semibold)
.padding()
}
.background(Color.orange)
.mask(RoundedRectangle(cornerRadius: 24))
.listRowPlatterColor(Color.clear)
}
}
推荐阅读
- python - 如何从用 np.poly1d 定义的多项式中获取值?
- ionic-framework - TypeError:无法读取 IONIC 5 中 null 的属性“canDeactivate”
- ios - (550 5.1.1 Relay not allowed APPLE)我无法从 gmail 地址向私人苹果地址发送电子邮件
- qt - QML 字体后备搜索
- ffmpeg - SIGABRT:中止尝试将 PCM 编码为 AAC
- javascript - 在我的 d3 图表中,当我更改画笔并单击未选择的范围时,缩放选择消失了
- javascript - Array.prototype.includes 函数如何比较对象
- java - 在 MYSQL 或 BigDecimal Java 中分钱
- php - 带有easyadmin的Symfony - 主页被重定向到管理页面
- angular - 如何使用角度材料将数据表导出为pdf