代码之家  ›  专栏  ›  技术社区  ›  StealthRT

将图像放置在列表中文本的左侧

  •  1
  • StealthRT  · 技术社区  · 3 年前

    我有以下代码,当前它给了我以下错误:

    enter image description here

    代码是:

    import SwiftUI
    
    struct EmailList: Identifiable {
        var img: Image
        var id: String
        var isOn = false
    }
    
    struct ContentView: View {
        @State var lists = [
            EmailList(img: Image(systemName: "car.fill"), id: "Monthly Updates", isOn: true),
            EmailList(img: Image(systemName: "car.fill"), id: "News Flashes", isOn: false),
            EmailList(img: Image(systemName: "car.fill"), id: "Special Offers", isOn: true)
        ]
        
        
        var body: some View {
            Form {
                Section {
                    ForEach($lists) { $list in
                        Toggle(list.img, list.id, isOn: $list.isOn)
                    }
                }            
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    它在手表上产生了这样的东西:

    enter image description here

    我不太确定它在说什么/想让我做什么。我看到的大多数例子都是这种格式的,所以我不确定这是因为它是一个只适用于iPhone的应用程序还是其他什么。

    所有的变量及其类都被填充,以将图像定义为第一个参数,然后是名称(id),最后是true或false。

    我的目标是让它看起来像这样:

    enter image description here

    1 回复  |  直到 3 年前
        1
  •  1
  •   Ashley Mills    3 年前

    您可以使用 Label 为您 Toggle 的标签,例如。

    struct ContentView: View {
        @State var lists = [
            EmailList(img: Image(systemName: "car.fill"), id: "Monthly Updates", isOn: true),
            EmailList(img: Image(systemName: "car.fill"), id: "News Flashes", isOn: false),
            EmailList(img: Image(systemName: "car.fill"), id: "Special Offers", isOn: true)
        ]
        
        
        var body: some View {
            Form {
                Section {
                    ForEach($lists) { $list in
                        Toggle(isOn: $list.isOn) {
                            Label {
                                Text(list.id)
                            } icon: {
                                list.img
                            }
                        }
                    }
                }
            }
        }
    }
    

    enter image description here