将水平卷轴移动到顶级Swiftui

发布于 2025-02-12 11:07:57 字数 2772 浏览 0 评论 0原文

我正在尝试将图像移至顶部中的图像。他们目前在底部。我尝试添加navigationView.padding()spacer(),添加zstack作为父母HSTACKhstack(Alignment:.top)。我被困了3天。

是否可以将水平滚动浏览在顶部?

这是代码:

import SwiftUI
struct ContentView: View {
    
    var body: some View {
        NavigationView {
            ScrollView(.horizontal) {
              
                
                    HStack(spacing: 15) {
                        Group {
                            Image("OutdoorDining1")
                            .frame(width: 230, height: 148)
                        
                        Image("EatVegan")
                            .frame(width: 230, height: 148)
                        
                        Image("CityView")
                            .frame(width: 230, height: 148)
                                
                        
                        Image("LiveMusic")
                            .frame(width: 230, height: 148)
                            }
                
                
                        Group {
                            Image("Seafood")
                                .frame(width: 230, height: 148)
                          
                
                            Image("Blackowned")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("Hookah")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("OpenTilLate")
                                .frame(width: 230, height: 148)
                            
                    
                            }
                
                        Group {
                            Image("CentralAmerica")
                                .frame(width: 230, height: 148)
                        
                    
                            Image("Asian")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("Mediterranean")
                                .frame(width: 230, height: 148)
                            
                    
                            }
                
                        Group {
                            Image("Steakhouses")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("Kosher")
                                .frame(width: 230, height: 148)
                           
                    
                            }
             

I am trying to move the images in Horizontal ScrollView up top. They are currently at the bottom. I have tried adding NavigationView, .padding(), Spacer(), Adding a ZStack as a parent to HStack, and HStack(alignment: .top). I have been stuck for 3 days.

Is it possible to have Horizontal ScrollView up top?

Here is the code:

import SwiftUI
struct ContentView: View {
    
    var body: some View {
        NavigationView {
            ScrollView(.horizontal) {
              
                
                    HStack(spacing: 15) {
                        Group {
                            Image("OutdoorDining1")
                            .frame(width: 230, height: 148)
                        
                        Image("EatVegan")
                            .frame(width: 230, height: 148)
                        
                        Image("CityView")
                            .frame(width: 230, height: 148)
                                
                        
                        Image("LiveMusic")
                            .frame(width: 230, height: 148)
                            }
                
                
                        Group {
                            Image("Seafood")
                                .frame(width: 230, height: 148)
                          
                
                            Image("Blackowned")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("Hookah")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("OpenTilLate")
                                .frame(width: 230, height: 148)
                            
                    
                            }
                
                        Group {
                            Image("CentralAmerica")
                                .frame(width: 230, height: 148)
                        
                    
                            Image("Asian")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("Mediterranean")
                                .frame(width: 230, height: 148)
                            
                    
                            }
                
                        Group {
                            Image("Steakhouses")
                                .frame(width: 230, height: 148)
                           
                    
                            Image("Kosher")
                                .frame(width: 230, height: 148)
                           
                    
                            }
             

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2025-02-19 11:07:57

嗨,克里斯蒂娜,欢迎来!您可以使用Chrisr的建议VSTACK + spacer() - 它是有效的,但是如果您构建了更复杂的UI并想添加scrollview(.ver​​tical)以及更多视图使用下一个层次结构

struct ContentView: View {
    var body: some View {
        ZStack(alignment: .top) {
            NavigationView {
                ScrollView(.vertical) {
                    VStack {
                        ScrollView(.horizontal) {
                            HStack(spacing: 15) {
                                Group {
                                    Image("EatVegan")
                                        .frame(width: 230, height: 148)
                                    
                                    Image("CityView")
                                        .frame(width: 230, height: 148)
                                }
                                
                                Group {
                                    Image("Seafood")
                                        .frame(width: 230, height: 148)
                                    
                                    Image("Blackowned")
                                        .frame(width: 230, height: 148)
                                }
                            }
                            
                        }
                        // add other views here
                    }
                }
            }
        }
    }
}

Hi Christina and welcome to SO! You can use ChrisR's suggestion VStack + Spacer() - it's works, but if you building more complex UI and want to add ScrollView(.vertical) and more views use next hierarchy

struct ContentView: View {
    var body: some View {
        ZStack(alignment: .top) {
            NavigationView {
                ScrollView(.vertical) {
                    VStack {
                        ScrollView(.horizontal) {
                            HStack(spacing: 15) {
                                Group {
                                    Image("EatVegan")
                                        .frame(width: 230, height: 148)
                                    
                                    Image("CityView")
                                        .frame(width: 230, height: 148)
                                }
                                
                                Group {
                                    Image("Seafood")
                                        .frame(width: 230, height: 148)
                                    
                                    Image("Blackowned")
                                        .frame(width: 230, height: 148)
                                }
                            }
                            
                        }
                        // add other views here
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文