Search code examples
iosswiftuipicker

SwiftUI SegmentedControl


I have a segmented control with two options. I want to show different views according to selectorindex. But for some reason I get warnings:

Result of 'PersonalAddressView' initializer is unused

and

Result of 'CorporateAddressView' initializer is unused

struct AddressSegment : View {
    @State private var selectorIndex = 0
    @State private var options = ["Bireysel","Kurumsal"]
    init() {
        UISegmentedControl.appearance().selectedSegmentTintColor = #colorLiteral(red: 0.05490196078, green: 0.3254901961, blue: 0.368627451, alpha: 1)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.init(displayP3Red: 0.05490196078, green: 0.3254901961, blue: 0.368627451, alpha: 1)], for: .normal)
    }
    var body: some View {
        VStack {
            Picker("Addresses", selection: $selectorIndex) {
                ForEach(0..<options.count) { index in
                    Text(self.options[index]).tag(index)
                }
            }
            .onReceive([self.selectorIndex].publisher.first(), perform: { value in
                if value == 0 {
                    PersonalAddressView()
                } else {
                    CorporateAddressView()
                }
            })
            .pickerStyle(SegmentedPickerStyle())
            .frame(width: 216, height: 28, alignment: .center)
            .cornerRadius(5)
            .foregroundColor(.white)
        }
    }
}

What I want is below enter image description here


Solution

  • You need to create views inside a ViewBuilder block, not in some function like onReceive.

    Try the following:

    struct AddressSegment : View {
        @State private var selectorIndex = 0
        @State private var options = ["Bireysel","Kurumsal"]
        init() {
            UISegmentedControl.appearance().selectedSegmentTintColor = #colorLiteral(red: 0.05490196078, green: 0.3254901961, blue: 0.368627451, alpha: 1)
            UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
            UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.init(displayP3Red: 0.05490196078, green: 0.3254901961, blue: 0.368627451, alpha: 1)], for: .normal)
        }
        var body: some View {
            VStack {
                Picker("Addresses", selection: $selectorIndex) {
                    ForEach(0..<options.count) { index in
                        Text(self.options[index]).tag(index)
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                .frame(width: 216, height: 28, alignment: .center)
                .cornerRadius(5)
                .foregroundColor(.white)
    
                // move the code here, to the ViewBuilder block
                if selectorIndex == 0 {
                    PersonalAddressView()
                } else {
                    CorporateAddressView()
                }
            }
        }
    }