在 SwiftUI 中更改分段控制器属性 [英] Change Segmented Controller properties in SwiftUI

查看:71
本文介绍了在 SwiftUI 中更改分段控制器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SwiftUI 中创建一个分段控制器.到目前为止,这是我的代码:

I'm trying to create a Segmented Controller in SwiftUI. This is my code so far:

Picker(selection: $currentStatus, label: Text("Treatment Status")) {
            ForEach(status, id: \.self) {
                Text($0)
            }
        }
        .pickerStyle(SegmentedPickerStyle())

这就是它的样子:

这很好,但我想更改分段控制器属性以使其看起来像这样:

This is fine, but I'd like to change the Segmented Controller properties to make it look like this:

我尝试查找有关如何在 SwiftUI 中修改分段控制器的任何教程,但找不到任何内容.任何帮助将不胜感激!

I tried looking up any tutorials on how to modify Segmented Controllers in SwiftUI but couldn't find anything. Any help would be highly appreciated!

推荐答案

这是一个与上面类似的视图,它的作用类似于一个选择器,看看这是否可行:

Here is a view that looks like the above and acts like a picker, see if this will work:

struct MinimalPickerView: View {
        @State var selected = "Pendiente"
        let labels = ["Pendiente", "Atendido", "Cancelado", "Ausente"]
    
        var body: some View {
            HStack {
                ForEach(labels, id: \.self) { label in
                    VStack {
                        Text(label).padding(2)
                        Rectangle()
                            .frame(height: 7)
                            .foregroundColor((selected == label) ? .black : .clear)
                    }
                    .onTapGesture {
                        selected = label
                    }
                    .padding(2)
                }
            }
        }

}

这篇关于在 SwiftUI 中更改分段控制器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆