问题
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
回答1:
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)
}
}
}
}
来源:https://stackoverflow.com/questions/63218071/typealias-lazyvstack-for-ios-13