首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI SegmentedControl

SwiftUI SegmentedControl
EN

Stack Overflow用户
提问于 2020-09-18 08:30:34
回答 1查看 89关注 0票数 1

我有一个有两个选项的分段控制。我想根据选择索引显示不同的观点。但出于某种原因我得到了警告:

”初始化器的PersonalAddressView结果未使用

”初始化器的CorporateAddressView结果未使用

代码语言:javascript
复制
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)
        }
    }
}

我想要的是下面

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-18 08:43:32

您需要在ViewBuilder块中创建视图,而不是像onReceive这样的函数。

尝试以下几点:

代码语言:javascript
复制
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()
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63952069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档