SwiftUI 차트를 만들고보기 내에서 사용되는 색상을 보관하기 위해 사전을 사용했습니다.
let ChartColors: [String: Color] = [
"title": Color.white,
"timeAgo": Color.red,
...
]
내에서 struct
의 body
변수 나는이를 같이 사용할 수 있습니다 :
struct Chart: View {
var body: some View {
Text("5min ago")
.foregroundColor(ChartColors["timeAgo"])
}
}
이러한 사전 값 을 변경하지 않으려는 경우 차트보기에서 이러한 기본값을 유지하면서 부모보기에서 이러한 사전 값을 사용자 지정할 수있는 가장 좋은 방법은 무엇입니까 ?
색상에 대한 구조체를 만들 수 있으며 기본값을 제공 할 수 있습니다.
struct ChartColors {
var title = Color.white
var timeAgo = Color.red
}
그런 다음 뷰에서 사용하십시오.
struct ContentView: View {
var body: some View {
let chartColors = ChartColors(title: .black, timeAgo: .green)
return VStack {
Chart(chartColors: chartColors) // with overridden colors
Chart() // with default colors
}
}
}
struct Chart: View {
var chartColors = ChartColors()
var body: some View {
Text("5min ago")
.foregroundColor(chartColors.timeAgo)
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다