可重用的视图与NavigationStack和可见单元格的大小/ SwiftUI / Swift 5

zpf6vheq  于 2023-05-05  发布在  Swift
关注(0)|答案(1)|浏览(121)

我想知道你是否可以帮我回答一个SwiftUI的问题。具体来说,我尝试创建一个可重用的视图,其中包含一个带有ForEach循环的NavigationStack,并且具有可调整的单元格大小。我可以让NavigationStack的大小仅对可见单元格动态变化,并全屏显示每个单元格吗?另外,考虑到我需要在单元格中重用视图,我找不到一个解决方案,如何使NavigationStack的背景颜色清晰。基本上,我只需要一个干净的背景和几个单元格,我可以点击并呈现全屏可重用的视图。任何提示或建议将不胜感激!

我的复用视图

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()
    }
}
3pvhb19x

3pvhb19x1#

几分钟后...))

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()
    }
}

非常感谢@loremipsum引导我走上正确的道路...))

相关问题