SwiftUI HStack 滑块不出现

SwiftUI HStack slider does not appear

我想水平堆叠图片。 不幸的是我无法滑动查看完整图像。


struct ContentView: View {
    var body: some View {
        NavigationView {
                List {

                    ScrollView {
                        VStack{
                            Text("Images").font(.title)
                        HStack {

                            Image("hike")
                            Image("hike")
                            Image("hike")
                            Image("hike")


                        }
                        }

                }.frame(height: 200)
            }
        }
    }
}

您的观点存在一些问题。

您的内容周围有一个列表 - 它会导致问题,因为列表是垂直滚动的,而我假设您希望图像水平滚动。

接下来您可能不希望标题随图像一起滚动 - 它需要超出滚动视图。

最后但同样重要的是,您需要使图像可调整大小并设置它们的纵横比,以便它们按比例缩小以适应分配的 space。

试试这个:

struct ContentView: View {

    var body: some View {
        NavigationView {
            VStack{
                Text("Images").font(.title)
                ScrollView(.horizontal) {
                    HStack {
                        Image("hike")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                        Image("hike")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                        Image("hike")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                        Image("hike")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                    } .frame(height: 200)
                    Spacer()
                }
            }
        }
    }
}