swift 为什么ScrollView会随机向下推送我的视图内容?

nuypyhwy  于 2023-01-25  发布在  Swift
关注(0)|答案(1)|浏览(133)

下面是我的代码:

import SwiftUI

struct TrendingView : View {
    
    @State var selectedTab: Tabs = .hot
    @State private var searchText = ""
    
    var body: some View {
        
        
        NavigationView {
            
            VStack {
                
                
                HStack {
                    
                    Text(" Trending")
                        .font(.largeTitle)
                        .frame(width: 175, height: 50, alignment: .leading)
                        .overlay(
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(.black, lineWidth: 2)
                        )
                        .italic()
                        .fontWeight(.heavy)
                        .foregroundColor(Color(hue: 1.0, saturation: 0.977, brightness: 0.985))
                    
                        .offset(x: -75, y: -25)
                        .padding(.bottom, -20)
                    
                    
                    NavigationLink(destination: testT()) {
                        Image(systemName: "magnifyingglass.circle.fill")
                            .resizable()
                            .scaledToFit()
                            .frame(width: 40, height: 40)
                            .foregroundColor(.black)
                            .padding(.bottom, -20)
                        
                        
                        
                    }
                    .offset(x: 50, y: -25)
                    .padding(.leading, 5.0)
                    

                }
                ScrollView {
                    VStack {
                        Text("Hot Items 🔥")
                        
                            .bold()
                            .italic()
                            .offset(x: 5)
                        
                            .frame(width: 120, height: 25, alignment: .leading)
                            .overlay(
                                RoundedRectangle(cornerRadius: 5)
                                    .stroke(.black, lineWidth: 2)
                            )
                            .background(.yellow)
                            .offset(x: -130, y: 5)
                        
                        
                        ScrollView(.horizontal) {
                            HStack {
                                NavigationLink(destination: ImageTest()) {
                                    Image("testpic1")
                                        .resizable()
                                        .frame(width: 200, height: 200)
                                }
                                NavigationLink(destination: ImageTest()) {
                                    Image("testpic3")
                                        .resizable()
                                        .frame(width: 200, height: 200)
                                }

            }.offset(y: 30)
            
            
        }
    }
}

我的vstack上面的scrollview似乎有一些错误,因为它随机导致这个视图上的所有内容在切换到其他视图时向下移动。我试过删除scrollview,错误消失了,也试过在其他地方改变它,但我仍然得到错误。有人能给我指出正确的方向吗?谢谢

hjzp0vay

hjzp0vay1#

修正,添加到我的代码底部,看起来问题已经解决了

.navigationBarTitle(Text(""), displayMode: .inline)
                .navigationBarHidden(true)

相关问题