SwiftUI 分段控制

Posted

技术标签:

【中文标题】SwiftUI 分段控制【英文标题】:SwiftUI SegmentedControl 【发布时间】:2020-09-18 08:30:34 【问题描述】:

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

“PersonalAddressView”初始值设定项的结果未使用

“CorporateAddressView”初始值设定项的结果未使用

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)
        
    

我想要的在下面

【问题讨论】:

【参考方案1】:

您需要在 ViewBuilder 块中创建视图,而不是在像 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()
            
        
    

【讨论】:

以上是关于SwiftUI 分段控制的主要内容,如果未能解决你的问题,请参考以下文章

自定义分段控制器 SwiftUI 框架问题

如何使用 SwiftUI 在 MacOS 上配置多选分段控制

SwiftUI - 带列表的段控制

SwiftUI 分段控件可定制

SwiftUI - 动态分段选取器过滤器

SwiftUI Segmented Picker 在点击时不会切换