どうも、ちょげ(@chogetarou)です。
SegmentedPickerStyleのPickerの選択されていないテキストの色を設定する方法を紹介します。
方法

SegementedPickerの選択中のテキストの色を設定するには、UISegmentedControlを使います。
まず、initメソッドやonAppear修飾子などで、「UISegmentedControl.appearance().setTitleTextAttributes()」を呼び出します。
そして、「setTitleTextAttributes」の第1引数に「[.foregroudColor : 色]」、第2引数「for」に「.normal」を指定します。
//.foregroundColorに続けて、色を指定
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor : color], for: .nomal)
UISegmentedControlを使うことで、セグメントのPickerの通常時のテキストカラーを設定することができます。
使用例

struct ContentView: View {
@State private var selectedIndex = 0
init() {
UISegmentedControl.appearance().setTitleTextAttributes(
[.foregroundColor : UIColor.blue], for: .normal
)
UISegmentedControl.appearance().setTitleTextAttributes(
[.foregroundColor : UIColor.red], for: .selected
)
}
var body: some View {
VStack {
Picker(selection: $selectedIndex, label: Text("Select").foregroundColor(.red)) {
ForEach(0..<5) {
Text("Item \($0)")
}
}
.pickerStyle(SegmentedPickerStyle())
}
}
}
コメント