Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typealias LazyVStack for iOS 13

Tags:

swift

swiftui

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 typealias would be perfect for this, but wasn't sure how to properly set this up so the alias is used on iOS 13 but not on 14. I tried this, but believe this would make the alias available on iOS 13 and up, so would include it on iOS 14 too.

Is there a way to set the availability to iOS 13 only? Or is there a better way to do this?

@available(iOS 13.0, *)
typealias LazyVStack = VStack
like image 934
keegan3d Avatar asked May 29 '26 12:05

keegan3d


1 Answers

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

struct CompatibleVStack<Content> : 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)
        }
      }
    }
}
like image 195
Asperi Avatar answered Jun 01 '26 04:06

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!