我創建了一個 RoundedRectangle 并將其 foregroundColor 設定為藍色
RoundedRectangle(cornerRadius: 10).foregroundColor(.blue)
我創建了一個自定義 SegmentedControl 結構并將其顏色設定為藍色
UISegmentedControl.appearance().selectedSegmentTintColor = .blue
但是,物件呈現為完全不同的藍色陰影。如何將它們設定為相同的藍色?
完整代碼:
import SwiftUI
struct ContentView: View {
@State var choice = Choice.a
var body: some View {
VStack {
Menu { } label: {
Text("Menu")
.foregroundColor(.white)
.padding(5)
.background(
RoundedRectangle(cornerRadius: 10)
.foregroundColor(.blue)
)
}
SegmentedControl(choice: $choice)
.padding(.horizontal, 50)
}
}
}
struct SegmentedControl: View {
@Binding var choice: Choice
var choices = [Choice.a, Choice.b]
init(choice: Binding<Choice>) {
UISegmentedControl.appearance().selectedSegmentTintColor = .blue
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)
self._choice = choice
}
var body: some View {
Picker("Which tab?",
selection: $choice,
content: {
ForEach(choices, id: \.self) {
Text($0.description())
}
})
.pickerStyle(.segmented)
}
}
enum Choice {
case a
case b
func description() -> String {
var str = ""
switch self {
case .a:
str = "Choice A"
case .b:
str = "Choice B"
}
return str
}
}
uj5u.com熱心網友回復:
不同之處在于Color.blue使用UIColor.systemBlue
所以你可以通過兩種方式來處理它
首先,您可以更改矩形
RoundedRectangle(cornerRadius: 10)
.foregroundColor(Color(UIColor.blue))
或第二次更改控制元件
UISegmentedControl.appearance().selectedSegmentTintColor = .systemBlue
這些更改中的任何一個都將匹配另一個。這取決于你喜歡哪個。
uj5u.com熱心網友回復:
您好,您可以在資產中創建一個“顏色集”,然后像這樣呼叫您的顏色:
RoundedRectangle(cornerRadius: 10).foregroundColor(Color("the name of your Color"))
UISegmentedControl.appearance().selectedSegmentTintColor = Color("the same Color")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/413449.html
標籤:
