首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不使用旧UISegmentControl的情况下改变swiftUI中分段控制的半径

如何在不使用旧UISegmentControl的情况下改变swiftUI中分段控制的半径
EN

Stack Overflow用户
提问于 2019-09-13 23:35:52
回答 2查看 820关注 0票数 4

我正在尝试更改SwiftUI中管段控制的半径。我只能更改外半径。有没有办法改变所选标签的半径?

代码语言:javascript
运行
复制
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])")
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-09-14 04:16:48

在这一点上,我认为没有办法修改所选标签的属性(例如形状)。我认为这要么是苹果在风格上的选择(因为他们想让标准控件看起来真正的“标准”),要么就是他们没有考虑到这一点(SwiftUI在这一点上很像是1.0 )。

票数 3
EN

Stack Overflow用户

发布于 2020-04-06 18:30:21

使用Introspection Library从SwiftUI视图访问底层UISegmentedControl。这样做,您可以对其进行自定义。下面是一个例子:

代码语言:javascript
运行
复制
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()
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57926555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档