ios - SwiftUI 分段控制

标签 ios swiftui picker

我有一个带有两个选项的分段控件。我想根据 selectorindex 显示不同的 View 。但由于某种原因,我收到警告:

Result of 'PersonalAddressView' initializer is unused



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)
        }
    }
}
我想要的在下面
enter image description here

最佳答案

您需要在 ViewBuilder block 中创建 View ,而不是像 onReceive 这样的函数。 .
尝试以下操作:

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

关于ios - SwiftUI 分段控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63952069/

相关文章:

SwiftUI:如何将图像转换为 jpeg 数据?

swift - 如何使用 SwiftUI 以编程方式转换 View ?

swift - 当我选择选择器时如何显示另一个(更改)ViewController

ios - 在 1 个 VC 中使用两个 UI Pickers,处理委托(delegate)方法

ios - 创建 iOS 开发证书

android - 我可以将 Xamarin.Android 移植到 Xamarin.ios 应用程序吗?

ios - 在目标ios中增加自定义本地通知声音的时间

ios - SwiftUI 截断某些字母的文本

android - Android中的日期选择器

ios - 解析 JSON 时奇怪的排序