我正在尝试更改SwiftUI中管段控制的半径。我只能更改外半径。有没有办法改变所选标签的半径?
struct ContentView: View {
@State private var favoriteColor = 0
var colors = ["Red", "Green", "Blue"]
var body: some View {
VStack {
Picker(selection: $favoriteColor, label: Text("What is your favorite color?")) {
ForEach(0..<colors.count) { index in
Text(self.colors[index]).tag(index)
}
}.pickerStyle(SegmentedPickerStyle())
.cornerRadius(13). ////////////////////////////////--
Text("Value: \(colors[favoriteColor])")
}
}
}发布于 2019-09-14 04:16:48
在这一点上,我认为没有办法修改所选标签的属性(例如形状)。我认为这要么是苹果在风格上的选择(因为他们想让标准控件看起来真正的“标准”),要么就是他们没有考虑到这一点(SwiftUI在这一点上很像是1.0 )。
发布于 2020-04-06 18:30:21
使用Introspection Library从SwiftUI视图访问底层UISegmentedControl。这样做,您可以对其进行自定义。下面是一个例子:
struct ContentView : View {
// 1.
@State private var selectorIndex = 0
@State private var numbers = ["One","Two","Three"]
var body: some View {
VStack {
// 2
Picker("Numbers", selection: $selectorIndex) {
ForEach(0 ..< numbers.count) { index in
Text(self.numbers[index]).tag(index)
}
}
.pickerStyle(SegmentedPickerStyle())
.introspectSegmentedControl{
segmentedControl in
segmentedControl.layer.cornerRadius = 0
segmentedControl.layer.borderColor = UIColor(red: 170.0/255.0, green: 170.0/255.0, blue: 170.0/255.0, alpha: 1.0).cgColor
segmentedControl.layer.borderWidth = 1.0
segmentedControl.layer.masksToBounds = true
segmentedControl.clipsToBounds = true
}
// 3.
Text("Selected value is: \(numbers[selectorIndex])").padding()
}
}
}https://stackoverflow.com/questions/57926555
复制相似问题