在tvOS上的SwiftUI中更改按钮的颜色 [英] Changing the color of a Button in SwiftUI on tvOS
问题描述
我正在尝试在tvOS上更改SwiftUI Button
的颜色.
I am trying to change the color of a SwiftUI Button
on tvOS.
修改 background
几乎可以用,除了您可以看到底层的 UIButton
实际上是在背景顶部使用了一个圆形的半透明图像.这会导致矩形背景位于圆角图像外部的角落处产生不同的颜色.
Modifying the background
almost works, except that you can see that the underlying UIButton
is actually using a rounded, translucent image over the top of the background. This results in a different colour at the corners where the rectangular background lies outside the rounded image.
添加 .padding
强调了此问题:
struct ContentView: View {
@State
var selected = false
var body: some View {
Button(action: {
self.selected.toggle()
}) {
Text($selected.wrappedValue ? "On":"Off")
.foregroundColor(.white)
}.padding(.all)
.background(self.selected ? Color.green : Color.blue)
}
}
}
一个相关的问题是更改焦点"视图的颜色,因为我怀疑这是与按钮本身相同的视图,改变了获胜大小和颜色.
A related issue is the changing the color of the "focus" view, as I suspect that this is the same view as the button itself, transformed win size and color.
带有 UIButton
的tvOS中的典型技术是更改按钮图像,但是似乎没有任何方法可以访问基础 UIButton
的图像.
The typical technique in tvOS with a UIButton
is to change the button image, but there doesn't seem to be any way to access the image of the underlying UIButton
.
推荐答案
同样,我仅在iOS上进行了检查,但这应该有所帮助:
Again, I have only checked it on iOS, but this should help:
struct ContentView: View {
@State var selected = false
var body: some View {
Button(action: { self.selected.toggle() }) {
Text($selected.wrappedValue ? "On":"Off")
.foregroundColor(.white)
} .padding(.all)
.background(RoundedRectangle(cornerRadius: 5.0)
.fill(self.selected ? Color.green : Color.blue))
}
}
您可以将任何视图传递到.background()修饰符,因此您可能还想尝试在其中放置Image().
You can pass any view into .background() modifier, so you might also want to try placing an Image() in there.
这篇关于在tvOS上的SwiftUI中更改按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!