我想在SwiftUI分割选择器中设置选定的分割颜色,并将文本颜色更改为白色。
我尝试将两种修饰符用于选择器 View ,还尝试从外观代理中修改色调颜色。不幸的是,它们似乎都不起作用。
import SwiftUI
struct PickerView: View {
@State var pickerSelection = 0
init() {
UISegmentedControl.appearance().tintColor = UIColor.blue
}
var body: some View {
Picker(selection: $pickerSelection, label: Text("")) {
Text("Active").tag(0).foregroundColor(Color.white)
Text("Completed").tag(1)
}.pickerStyle(SegmentedPickerStyle()).foregroundColor(Color.orange)
}
}
在SwiftUI中有什么方法可以做到这一点,还是应该只使用UIViewControllerRepresentable来使用UISegmentedControl?
最佳答案
从beta 3开始,可以使用selectedSegmentTintColor
更改所选段的颜色。
要更改textColor
,应将setTitleTextAttributes
用于.selected
状态和.normal
状态(未选中)。
因此它将像:
init() {
UISegmentedControl.appearance().selectedSegmentTintColor = .blue
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.blue], for: .normal)
}
关于ios - 如何在SwiftUI分段选择器中更改选定的分段颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57735761/