0

I was wondering if you could help me with a SwiftUI question. Specifically, I'm trying to create a reusable view that includes a NavigationStack with ForEach loop and has adjustable cells sizes. Can I make the size of NavigationStack to be dynamic only for visible cells and present every cell on full screen? Also, given that I need to reuse my view with my cells, I can't find a solution how to make the background color to be clear for NavigationStack. Basically, I just need a reusable view with a clean background and a few cells which I can click and present to fullscreen. Any tips or suggestions would be greatly appreciated!

My reusable view


import SwiftUI

struct TodaysHolidaysView: View {

    var holidays: [HolidaysModel] = HolidayList.mockData

    var body: some View {
    
        NavigationStack {
        
            VStack(spacing: 7.5) {
                ForEach(holidays, id: \.id) { holiday in
                    NavigationLink {
                        HolidayDetailView(holiday: holiday)
                    } label: {
                        HolidayCell(holiday: holiday)
                    }
                }
            
            }
            .frame(height: .infinity)
        }
        .cornerRadius(15)
        .padding(.all, 10)
    }
}

struct TodaysHolidaysView_Previews: PreviewProvider {
    static var previews: some View {
        TodaysHolidaysView()
    }
}

Gucci
  • 47
  • 1
  • 7
  • SwiftUI.Grid might be what you are looking for – lorem ipsum Apr 28 '23 at 18:26
  • @loremipsum Hello! Thanks for the answer! I did not quite understand what you mean. I already have a table with cells, the only thing I can't do is resize the NavigationStack to match the displayed cells and make the stack color clear. – Gucci Apr 28 '23 at 18:39
  • @loremipsum And in this case, as I understand it, I can't get rid of the NavigationStack because I need navigation for each cell... – Gucci Apr 28 '23 at 18:44
  • NavigationStack should never be resized, it should be at the very top of the app. Most apps only need one, there are few exceptions such as TabView and sheets. – lorem ipsum Apr 28 '23 at 18:44
  • @loremipsum I am wondering if it is possible to use NavigationLink without NavigationStack to navigate to each cell. Is there a way to achieve this? – Gucci Apr 28 '23 at 18:48
  • The NavigationStack should be at the very top. Just 1, it works for the whole app. – lorem ipsum Apr 28 '23 at 18:49
  • @loremipsum Got confused. I thought SwiftUI was quite flexible in this matter. Thank you! – Gucci Apr 28 '23 at 18:55
  • If you use more than one you will have a ton of UI issues. – lorem ipsum Apr 28 '23 at 18:57
  • @loremipsum Thanks a lot for the clarification on NavigationStack. In this case, I confused myself, and then solved the problem with a regular button and .sheet(item: $selectedHoliday) {}... It's hard to dive into SwiftUI after UIKit…)) Thanks a lot!!!) – Gucci Apr 28 '23 at 19:57
  • It is quite a difference, the NavigationStack is a UIViewController. Sheet is “present” navigation links are “show” – lorem ipsum Apr 28 '23 at 20:08

1 Answers1

0

A few moments later...))


import SwiftUI

struct TodaysHolidaysView: View {

    @State private var selectedHoliday: HolidaysModel?
    
    var holidays: [HolidaysModel] = HolidayList.mockData
    
    var body: some View {
        
        VStack {
            ForEach(holidays, id: \.id) { holiday in
                Button {
                    self.selectedHoliday = holiday
                } label: {
                    HolidayCell(holiday: holiday)
                }
            }
        }
        .frame(height: .infinity)
        .padding(.horizontal, 10)
        .sheet(item: $selectedHoliday) { holiday in
            HolidayDetailView(holiday: holiday)
        }
    }
}

struct TodaysHolidaysView_Previews: PreviewProvider {
    static var previews: some View {
        TodaysHolidaysView()
    }
}

Big thanks to @loremipsum for guiding me to the right path...))

Gucci
  • 47
  • 1
  • 7