如何使用NavigationView和NavigationLink

时间:2019-08-20 12:59:45

标签: swift swiftui

我正在为Item设置NavigationLink,但无法处理消失的图像。

struct Items: View {

    var landmarks: [Landmark]

    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack {
                ForEach(landmarks) { landmark in
                    //加了这个导航就看不到图片了,CategoryHome 也不显示图片,
                    //NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                        Item(landmark: landmark)
                    //}
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您必须将Scrollview嵌入到NavigationView中,否则它将无法正常工作

struct Items: View {

var landmarks: [Landmark]

var body: some View {
    NavigationView {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack {
                ForEach(landmarks) { landmark in
                    //加了这个导航就看不到图片了,CategoryHome 也不显示图片,
                    NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                        Item(landmark: landmark)
                    }
                }
            }
        }
    }
}

}

Here是用于创建导航的苹果教程,SwiftUI