Home > Software engineering >  How to remove blank space between two text views in SwiftUI
How to remove blank space between two text views in SwiftUI

Time:02-10

I have a problem with a Text view. I have a List of menu products, and the products contains a description. But, when a product don't have a description, there is a blank space. I do not want the blank space. How can I remove the blank space ? This is what I Have : [First Picture] This is what I want : Second Picture

I will share the code Below :

struct SectionVieww: View {
    @EnvironmentObject var syncViewModel : SyncViewModel
    @StateObject var coreDataViewModel = CoreDataViewModel()
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(sortDescriptors: [])
    private var menus : FetchedResults<LocalMenu>
    var menuType : MenuType
    var body: some View {
        let filteredMenus = syncViewModel.menu
            .filter({$0.type == menuType.id})
        if !filteredMenus.isEmpty {
            Section(header:
                        Text("\(menuType.text.capitalized)")
                        .font(.system(size: 20, weight: .bold, design: .rounded))
                        .foregroundColor(.black)
            ) {
                ForEach(filteredMenus) { men in
                    HStack {
                        WebImage(url: men.image.getThumbnailUrl(), options: .refreshCached)
                          .onFailure(perform: { (error) in
                          } )
                                .resizable()
                                .frame(width: 80, height: 80)
                                .cornerRadius(10)
                            VStack(alignment: .leading, spacing: 10) {
                                Spacer()
                                Text(men.name)
                                    .font(.system(size: 16))
                                    .foregroundColor(.black)
                                    .fontWeight(.semibold)
                                
                                Text(men.welcomeDescription ?? "")
                                    .font(.caption)
                                    .foregroundColor(.gray)
                                HStack {
                                    Text("\(men.price) lei")
                                        .fontWeight(.bold)
                                    Text("\(men.ingredients.map({$0.grams}).reduce(0,  ), specifier: "%.0f")g")
                                        .font(.system(size: 16))
                                        .fontWeight(.medium)
                                        .foregroundColor(.gray)
                                }
                                Spacer()
                            }
                        
                        Spacer()
                        Button {
                                coreDataViewModel.addTask(name: men.name, grams: men.ingredients.map({$0.grams}).reduce(0,  ), price: Int((men.price)), id: men.id)
                        } label: {
                            Image(systemName: "plus")
                                .font(.title)
                                .foregroundColor(Color.onboardingColor)
                        }
                        .buttonStyle(PlainButtonStyle())
                    }.background(NavigationLink("", destination:
                                ItemMenuView(meniu: men)
                                                    .toolbar {
                                                        ToolbarItem(placement: .navigationBarTrailing) {
                                                            ToolbarButtons(numberOfProducts: menus.count)
                                                    }
                    }
                                               ).opacity(0)
                    )
            }
            }
            
        } else {
            EmptyView()
        }
    }
    
}

struct MeniuriView: View {
    @EnvironmentObject var syncViewModel : SyncViewModel
   
    var body: some View {
     
        List  {
            ForEach(syncViewModel.menuType) { type in
                SectionVieww(menuType: type)     
            }
        }
        .listStyle(PlainListStyle())
        
    }
            }

CodePudding user response:

Put it conditionally, like

if let description = men.welcomeDescription {
  Text(description)
    .font(.caption)
    .foregroundColor(.gray)
}
  • Related