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

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

struct ContentView: View {
@State private var selectedIndex = 0
init() {
//赤色に設定
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())
}
}
}
コメント