swiftui列表未显示

发布于 2025-02-08 05:59:38 字数 2280 浏览 2 评论 0原文

我刚刚开始学习Swiftui和WatchOS,所以在线玩一些列表示例以学习它。最终,我希望列出一个列表,排在左侧显示图像,并在右侧显示文字。我的代码下面显示了行的框架,但未在观察模拟器上显示内容。

contentView代码:

import SwiftUI

struct ContentView: View {
    @Namespace var namespace
    @State var selected: [MenuItem] = []
    
    var body: some View {
        MainMenuListView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}

main -menulistview代码:

import SwiftUI

struct MainMenuListView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                Text("Hello World")
                
                List {
                    Text("item.name")
                        .font(.headline)
                        .listRowInsets(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
                        .listRowPlatterColor(.clear)
                }.padding()

                // Example 2
//                List {
//                    Button(action: {}) { Text("Testing Testing") }
//                                .buttonStyle(BtnStyle(bgColor: .red, fgColor: .white))
//                                .listRowInsets(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
//                                .listRowPlatterColor(.clear)
//                }.padding()
            }.navigationTitle("Sample Application")
        }
    }
}

struct MainMenuListView_Previews: PreviewProvider {
    static var previews: some View {
        MainMenuListView()
    }
}

// Button Styles
struct BtnStyle: ButtonStyle {
    var bgColor: Color
    var fgColor: Color

    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .frame(minWidth: 0, maxWidth: .infinity)
            .padding()
            .foregroundColor(fgColor)
            .background(bgColor)
            .cornerRadius(32)
    }
}

我将写入代码以在列表行上显示图像和文本。但是为什么什么都没有 用我拥有的代码出现在模拟器中?这是观看模拟器上的外观。

I just started learning SwiftUI and WatchOS, so playing with some list examples online to learn it. Eventually I wish to make a list with rows showing image on the left and text besides it on the right. My code below shows the frame of the row, but does not display the content on Watch simulator.

ContentView Code:

import SwiftUI

struct ContentView: View {
    @Namespace var namespace
    @State var selected: [MenuItem] = []
    
    var body: some View {
        MainMenuListView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            ContentView()
        }
    }
}

MainMenuListView Code:

import SwiftUI

struct MainMenuListView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                Text("Hello World")
                
                List {
                    Text("item.name")
                        .font(.headline)
                        .listRowInsets(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
                        .listRowPlatterColor(.clear)
                }.padding()

                // Example 2
//                List {
//                    Button(action: {}) { Text("Testing Testing") }
//                                .buttonStyle(BtnStyle(bgColor: .red, fgColor: .white))
//                                .listRowInsets(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 0))
//                                .listRowPlatterColor(.clear)
//                }.padding()
            }.navigationTitle("Sample Application")
        }
    }
}

struct MainMenuListView_Previews: PreviewProvider {
    static var previews: some View {
        MainMenuListView()
    }
}

// Button Styles
struct BtnStyle: ButtonStyle {
    var bgColor: Color
    var fgColor: Color

    func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .frame(minWidth: 0, maxWidth: .infinity)
            .padding()
            .foregroundColor(fgColor)
            .background(bgColor)
            .cornerRadius(32)
    }
}

I will write the code to display image and text on a List row. But why is nothing
showing up in the simulator with the code I have? Here is how it looks on watch simulator.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文