简体   繁体   English

SwiftUI Segmented Picker在点击时不切换

[英]SwiftUI Segmented Picker does not switch when click on it

I'm trying to implement Segmented Control with SwiftUI.我正在尝试使用 SwiftUI 实现分段控制。 For some reason Segmented Picker does not switch between values when click on it.由于某种原因,分段选择器在单击时不会在值之间切换。 I went through many tutorials but cannot find any difference from my code:我浏览了许多教程,但找不到与我的代码有任何区别:

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()
        }
    }
}

Would appreciate any suggestions!任何建议将不胜感激!

Update: The issue seem to have occured due to overlapping of the View 's because the cornerRadius value of RoundedRectangle was set to 100. Bringing the value of cornerRadius to 55 would restore the Picker 's functionality.更新:问题似乎是由于View的重叠而发生的,因为 RoundedRectangle 的cornerRadius 值设置为 100。将cornerRadius 的值设置为 55 将恢复Picker的功能。

The issue seems to be caused because of the RoundedRectangle(cornerRadius: 100) within the ZStack .该问题似乎是由于ZStack中的RoundedRectangle(cornerRadius: 100)引起的。 I don't have and explanation as to why this is happening.我没有解释为什么会这样。 I'll add the reason if I ever find out.如果我发现了,我会添加原因。 Could be a SwiftUI bug.可能是 SwiftUI 错误。 I can't tell untill I find any related evidence.在找到任何相关证据之前我无法确定。 So here is the code that could make the SegmentedControl work without any issue.所以这里是可以使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()
        }
    }
}

Your code/View is missing the if-condition to know what shall happen when the selectorIndex is 0 or 1.您的代码/视图缺少 if 条件来了解当 selectorIndex 为 0 或 1 时会发生什么。

It has to look like this:它必须看起来像这样:

var body: some View {

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

Xcode 12 iOS 14 you can get it by adding a if-else condition on the tapGesture of your Segment Control(Picker) Xcode 12 iOS 14 您可以通过在您的段控制(选择器)的 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()

If you need to use it with more than 2 segments, you can try with an Enum:)如果您需要将它与 2 个以上的段一起使用,您可以尝试使用 Enum :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM