11.1 C
London
Friday, December 8, 2023

ios – ScrollView Not Taking Full Width When Checklist Inside Is Empty


I’ve created a listing inside a scroll view that works fantastic when it has objects, however I’ve seen that when there aren’t objects within the checklist that the scroll view may be very slim and it makes it arduous to make use of the refreshabled. I’ve tried many options on-line equivalent to utilizing further spacers, GeometryReady width, and different issues however nothing appears to repair it.

Right here is my code:

var physique: some View {
    GeometryReader { geo in
        ZStack {
            LinearGradient(stops: [
                Gradient.Stop(color: Color("Cottage Green Tertiary"), location: 0.0),
                Gradient.Stop(color: Color("Cottage Green Tertiary"), location: 0.85),
                Gradient.Stop(color: Color("Cottage Dark Green"), location: 1.0)
            ], startPoint: .prime, endPoint: .backside)
            .edgesIgnoringSafeArea(.all)
            
            ScrollView(.vertical) {
                ForEach(Array(self.viewModel.posts.enumerated()), id: .aspect) { index, put up in
                    VStack {
                        HStack {
                            if self.viewModel.canDeletePost(postIndex: index) {
                                Button {
                                    self.viewModel.latestPostIndexSelectedToDelete = index
                                    self.isShowingDeleteConfirm = true
                                } label : {
                                    Picture(systemName: "trash")
                                        .foregroundStyle(Coloration("Cottage Darkish Inexperienced"))
                                }
                            }
                            Spacer()
                            Textual content(getDateDescription(from: put up.datePosted))
                                .font(.system(dimension: 12))
                                .padding(.backside, 10)
                        }
                        Textual content(put up.message)
                            .body(maxWidth: .infinity, alignment: .main)
                            .padding(.backside, 10)
                        HStack {
                            Picture(systemName: "individual.fill")
                                .foregroundStyle(Coloration("Cottage Darkish Inexperienced"))
                            Textual content(put up.poster.identify)
                            Spacer()
                            Button {
                                let impactMed = UIImpactFeedbackGenerator(type: .medium)
                                impactMed.impactOccurred()
                                if put up.likers.accommodates(the place: { $0 == self.userId }) {
                                    self.viewModel.removeLike(from: self.userId, for: index)
                                } else {
                                    self.viewModel.addLike(from: self.userId, for: index)
                                }
                            } label : {
                                if put up.likers.accommodates(the place: { $0 == self.userId }) {
                                    Picture(systemName: "hand.thumbsup.fill")
                                        .foregroundStyle(Coloration("Cottage Darkish Inexperienced"))
                                } else {
                                    Picture(systemName: "hand.thumbsup")
                                        .foregroundStyle(Coloration("Cottage Darkish Inexperienced"))
                                }
                            }
                            Textual content(String(put up.likers.depend))
                        }
                    }
                    .padding([.top, .bottom, .leading, .trailing], 10)
                    .background(Coloration("Cottage Inexperienced"))
                    .cornerRadius(10)
                }
                .padding([.top, .bottom, .leading, .trailing], 10)
                .cornerRadius(10)
            }
            .body(width: geo.dimension.width)
            .alert(isPresented: $isShowingDeleteConfirm) {
                Alert(
                    title: Textual content("Delete Put up?"),
                    message: nil,
                    primaryButton: .harmful(Textual content("Delete")) {
                        self.viewModel.deletePost()
                    },
                    secondaryButton: .cancel()
                )
            }
            .refreshable {
                print("Do your refresh work right here")
                do {
                    strive await viewModel.getPosts()
                } catch {
                    
                }
            }
            
            if self.loadingState.isLoading {
                ProgressView()
            }   
        } 
    }
}

Here’s what it seems like when it’s empty:
enter image description here

Any assistance is appreciated as I’m pretty new to SwiftUI! Thanks!

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here