SwiftUI分段选择器在点击时无法切换。

6

我正在尝试使用SwiftUI实现分段控件。但是,对于某些原因,当单击它时,分段选择器不能在值之间切换。我查看了许多教程,但无法从我的代码中找到任何区别:

struct MeetingLogin: View {
    @State private var selectorIndex: Int = 0

    init() {
        UISegmentedControl.appearance().backgroundColor = UIColor(named: "JobsLightGreen")
        UISegmentedControl.appearance().selectedSegmentTintColor = UIColor(named: "AlmostBlack")
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white,
                                                                .font: UIFont(name: "OpenSans-Bold", size: 13)!],
                                                               for: .selected)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white,
                                                                .font: UIFont(name: "OpenSans-Regular", size: 13)!],
                                                               for: .normal)
    }

    var body: some View {
        VStack {

            ...

            Group {
                Spacer().frame(minHeight: 8, maxHeight: 30)
                Picker("video", selection: $selectorIndex) {
                    Text("Video On").tag(0)
                    Text("Video Off").tag(1)
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding([.leading, .trailing], 16)

                Spacer().frame(minHeight: 8, maxHeight: 50)
                Button(action: { self.sendPressed() }) {
                    ZStack {
                        RoundedRectangle(cornerRadius: 100)
                        Text("go")
                            .font(.custom("Roboto-Bold", size: 36))
                            .foregroundColor(Color("MeetingGreen"))
                    }
                }
                .foregroundColor(Color("MeetingLightGreen").opacity(0.45))
                .frame(width: 137, height: 73)
            }
            Spacer()
        }
    }
}

希望能得到任何建议!

3个回答

5
更新:问题似乎是由于重叠的View引起的,因为RoundedRectangle的cornerRadius值设置为100。将cornerRadius的值调整为55可以恢复Picker的功能。
问题似乎是因为在ZStack中使用了RoundedRectangle(cornerRadius: 100)导致的。我不知道为什么会出现这种情况。如果我找到原因,我会添加说明的。可能是SwiftUI的bug,除非我找到任何相关证据,否则无法确定。所以,以下是可以让SegmentedControl正常工作的代码。
struct MeetingLogin: View {
    //...
    @State private var selectorIndex: Int = 0

    var body: some View {
        VStack {
            //...
            Group {
                Spacer().frame(minHeight: 8, maxHeight: 30)
                Picker("video", selection: $selectorIndex) {
                    Text("Video On").tag(0)
                    Text("Video Off").tag(1)
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding([.leading, .trailing], 16)

                Spacer().frame(minHeight: 8, maxHeight: 50)
                Button(action: { self.sendPressed() }) {
                    ZStack {
                        RoundedRectangle(cornerRadius: 55)
                        Text("go")
                            .font(.custom("Roboto-Bold", size: 36))
                            .foregroundColor(Color("MeetingGreen"))
                    }
                }
                .foregroundColor(Color("MeetingLightGreen").opacity(0.45))
                .frame(width: 137, height: 73)
            }
            Spacer()
        }
    }
}

哇,那真是奇怪的东西!谢谢你的帮助! - dandepeched
@dandepeched 嗯,伙计,我也是这么想的。我已经点赞了,希望这篇文章能得到一些关注。 - Frankenstein
进一步调查显示问题在于cornerRaduis值。当设置为50时,Picker可以正常工作,并且按钮看起来完全相同(因为frame纵横比)。因此,我认为这是UI重叠或类似问题的一些错误。请将此信息添加到您的答案中。 - dandepeched
是的,在圆角矩形上注册了触摸。 - Frankenstein

2

Xcode 12 iOS 14 您可以通过在分段控制器(Picker)的tapGesture上添加if-else条件来获取它。

@State private var profileSegmentIndex = 0
Picker(selection: self.$profileSegmentIndex, label: Text("Jamaica")) {
                    Text("My Posts").tag(0)
                
                Text("Favorites").tag(1)
            }
            .onTapGesture {
                if self.profileSegmentIndex == 0 {
                    self.profileSegmentIndex = 1
                } else {
                    self.profileSegmentIndex = 0
                }
            }
            .pickerStyle(SegmentedPickerStyle())
            .padding()

如果您需要将其与两个以上的段一起使用,可以尝试使用枚举 :)

0

你的代码/视图缺少if条件,无法确定当selectorIndex为0或1时应该发生什么。

它应该像这样:

var body: some View {

    VStack {
        if selectorIndex == 0 {
    //....Your VideoOn-View 
    } else {
    //Your VideoOff-View
    }
}

不需要使用“if”条件语句,SwiftUI Picker可以正常工作。 - dandepeched
Huhlemann建议使用所选的选择器索引来有条件地显示视图,当选择了特定的索引时。他是正确的。也许他可以在他的示例中包含有效的选择器代码,但他的观点是非常准确的,那么为什么要对他进行负面评价呢? - Mozahler
他的提议与这个特定问题无关。 - dandepeched

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接