SwiftUI - ScrollView 没有一直滚动到底部

Posted

技术标签:

【中文标题】SwiftUI - ScrollView 没有一直滚动到底部【英文标题】:SwiftUI - ScrollView not scrolling all the way to the bottom 【发布时间】:2020-02-29 20:21:11 【问题描述】:

我无法让我的滚动视图一直滚动到底部。奇怪的是,当它填充一组数据时,它没有这个问题,但是,在另一个数据集中它会到达最后一项,我无法滚动到 HStack 的下半部分。

这是视图的代码

import SwiftUI

struct FestivalDescription: View 
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

var passportTitle: String
var passportImageVert: String
var venues: [Venue]

var body: some View 
    ZStack 
        ScrollView(.vertical, showsIndicators: false) 
            GeometryReader  geometry in
                ZStack
                    if geometry.frame(in: .global).minY <= 0 
                        Image(self.passportImageVert)
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: geometry.size.width, height: geometry.size.height)
                            .offset(y: geometry.frame(in: .global).minY/9)
                            .clipped()
                     else 
                        Image(self.passportImageVert)
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: geometry.size.width, height: geometry.size.height + geometry.frame(in: .global).minY)
                    
                    VStack 
                        Image("script_passport")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: geometry.size.width * 0.75)
                        Text(self.passportTitle)
                            .fontWeight(.bold)
                            .foregroundColor(Color.white)
                            .padding(.top, 10)

                        // Insert Navigation Link

                        NavigationLink(destination: FestivalDetails(passportTitle: self.passportTitle, venues: self.venues, venueProd: [])) 
                            HStack 
                                Text("View all Food & Drinks")
                                    .font(.subheadline)
                                    .padding()
                            .frame(width: 210, height: 50)
                                .background(Color.white)
                                .cornerRadius(4)
                                .padding(.top, 8)
                        
                        // End Button
                    
                
            .frame(height: UIScreen.main.bounds.height * 0.63)
                .clipped()// Ends GeometryReader
            VStack 
                HStack 
                    Text("Festival Booths")
                        .font(.title)
                        .fontWeight(.bold)
                    Spacer()
                .padding(.top, 24)
                    .padding(.bottom, 16)
                ForEach(self.venues)  venue in
                    NavigationLink(destination: VenueDetails(title: venue.title,  venueImage: venue.venueImage, venueDesc: venue.venueDesc, venueArea: venue.venueArea, venueLat: venue.venueLat, venueLong: venue.venueLong, venueProd: venue.venueItems)) 
                        HStack 
                            VStack 
                                Image(venue.venueImage)
                                    .renderingMode(.original)
                                    .resizable()
                                    .frame(minWidth: 0, maxWidth: 366, minHeight: 0, maxHeight: 300)
                                    .scaledToFill()

                                HStack 
                                    Text(venue.title)
                                        .font(.body)
                                        .fontWeight(.semibold)
                                        .foregroundColor(Color("Charcoal"))
                                    Spacer()
                                
                                HStack(alignment: .top) 
                                    Text(venue.venueDesc)
                                        .font(.footnote)
                                        .foregroundColor(Color("bodyText"))
                                        .multilineTextAlignment(.leading)
                                        .lineLimit(2)
                                        .frame(height: 40)
                                    Spacer()
                                
                                Spacer()
                            
                        
                    .padding(.bottom, 16)
                
                // End VStack of Venues
               // Spacer()
            .padding(.horizontal, 24)
            .background(Color("bodyBackground"))

            // Ends Bottom Layer VStack
        
            .edgesIgnoringSafeArea(.top) // Ends ScrollView
        .background(Color("bodyBackground"))

        ZStack 
            GeometryReader  geometry in
                ZStack 

                    VStack 
                        Text("")
                            .frame(width: geometry.size.width, height: 120)
                            .background(Color("navBackground"))
                            .opacity(0.3                     )
                        Spacer()
                    

                .edgesIgnoringSafeArea(.top) // Ends ZStack
             // Ends Geometry Reader
            VStack 
                GeometryReader  gr in
                    HStack 
                        Button(action: self.presentationMode.wrappedValue.dismiss()) 
                            Image(systemName: "chevron.left")
                                 .foregroundColor(Color("Charcoal"))
                                .padding(.leading, 16)
                            HStack 
                                Text("Passports · Passport Details")
                                    .font(.system(size: 15))
                                    .fontWeight(.bold)
                                     .foregroundColor(Color("Charcoal"))

                                    .padding()
                                Spacer()
                            
                        .frame(width: gr.size.width * 0.92, height: 48)
                            .background(Color("navBackground"))
                            .cornerRadius(8)
                        .shadow(color: Color("Shadow"), radius: 10, x: 2, y: 7)
                    .padding(.leading, 16)
                    Spacer()
                
            
            .padding(.top, 50)
            .edgesIgnoringSafeArea(.top)
         // Ends Floating Menu ZStack

     // Ends Main ZStack
 


【问题讨论】:

好吧,实际上,这段代码不可读 - 尝试在几个子视图上将其分解,但我认为问题是由于填充、大小、高度等中的许多硬编码值造成的。跨度> 【参考方案1】:

原来我的问题是图像上的纵横比修饰符。我将其设置为适合而不是填充,从而解决了问题。

【讨论】:

以上是关于SwiftUI - ScrollView 没有一直滚动到底部的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI ScrollView 没有更新?

iOS SwiftUI:使用 ScrollView 和 ZStack 正确堆叠 NavigationView?

SwiftUI ScrollView、GeometryReader 和 Button = 奇怪的行为

ScrollView 内容未填写 SwiftUI

SwiftUI - 如何获取 ScrollView 内容的大小(高度)

ScrollView 文本对齐 SwiftUI