Reputation: 294
How do I make it so the brightness only affects the view's background color and not its subviews? I want the text to stay white, but the background to get darker
this code
struct ListCell: View {
let brightness : Double
var body: some View {
VStack {
HStack {
Image(systemName: "music.house")
.resizable()
.aspectRatio(1, contentMode: .fit)
.frame(height: 50)
.padding()
VStack(alignment: .leading) {
Text("List Name")
.padding(.bottom,5)
.foregroundColor(Color.white)
Text("A long subtitle which extends more than a line")
.foregroundColor(Color.white)
}
}.background(Color.blue).brightness(brightness)
}
}
}
and this code
struct ContentView: View {
@State var lists : [Listi] = theLists
var body: some View {
var color = 0.0
List(lists) { list in
ListCell(brightness: color).onAppear(perform: {
color -= 0.1
})
}
.onAppear(perform: {
UITableView.appearance().separatorColor = .clear
})
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Upvotes: 3
Views: 4843
Reputation: 30278
If you do:
HStack {
...
}
.background(Color.blue)
.brightness(brightness)
The order matters, so you can think of it as
... you're applying the brightness modifier to the HStack + blue background
. Instead, if you only want the blue background to get the brightness modifier, just attach it like this instead:
HStack {
...
}
.background(
Color.blue
.brightness(brightness)
)
Color
conforms to View
, so you are allowed to add view modifiers like brightness
to it.
Upvotes: 6