typealias LazyVStack for iOS 13

前端 未结 1 941
温柔的废话
温柔的废话 2021-01-22 23:35

I have a SwiftUI app that I want to maintain iOS 13 support for, but on iOS 14 I want to use the new LazyVStack and LazyHStack. I was thinking ty

1条回答
  •  不知归路
    2021-01-22 23:59

    Here is possible wrapper that can be used as regular stack container

    struct CompatibleVStack : View where Content : View {
        let alignment: HorizontalAlignment
        let spacing: CGFloat?
        let content: () -> Content
    
        init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil,
                @ViewBuilder content: @escaping () -> Content) {
            self.alignment = alignment
            self.spacing = spacing
            self.content = content
        }
    
        var body: some View {
          Group {
            if #available(iOS 14, *) { // << add more platforms if needed
                LazyVStack(alignment: alignment, spacing: spacing, pinnedViews: [], content:content)
            } else {
                VStack(alignment: alignment, spacing: spacing, content:content)
            }
          }
        }
    }
    

    0 讨论(0)
提交回复
热议问题