Charlie Page
Charlie Page

Reputation: 831

issue with if #available SwiftUI less than iOS 14

I'm using the beta version of Xcode 12.0 to play around with LazyVGrid, to render this grid within a scrollview if the phone has iOS 14, otherwise just to render the ScrollView as one column.

When I launch this on the app on my phone (not using iOS 14), opening this view causes my app to crash. But if I comment out the "if #available" section and just display what's in the "else" statement, it works fine.

Is there an issue with if #available in earlier versions of iOS or is my syntax just incorrect?

    var body: some View {
        NavigationView {
            VStack {
                //Empty View navigation link to choose the selected pack in User Defaults.
                ScrollView(.vertical, showsIndicators: false) {
                    //Checks if iOS version 14.0 is available to render the lazy grid view
                    if #available(iOS 14.0, *) {

                        LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 15) {
                                //checks if the pack is in the purchased list - if so, renders it as an unlocked tile.
                            ForEach((allPacks), id: \.self) { pack in
                                    UnlockedPackTile(tilePack: pack)
                                        .onTapGesture {
                                            print("Originally tapped \(pack.name)")
                                            self.userInformation.defaultPack = pack
                                            self.isPresented.toggle()
                                }
                            }
                        }
                    } else {
                            //does this as a simple stack instead if iOS 14 is not available.
                            ForEach((allPacks), id: \.self) { pack in
                                    UnlockedPackTile(tilePack: pack)
                                        .onTapGesture {
                                            print("Originally tapped \(pack.name)")
                                            self.userInformation.defaultPack = pack
                                            self.isPresented.toggle()
                            }
                        }
                    }
                }

Upvotes: 2

Views: 1004

Answers (1)

Asperi
Asperi

Reputation: 257779

Try to wrap content of ScrollView into Group (or VStack) like

ScrollView(.vertical, showsIndicators: false) {
   Group {
      if #available(iOS 14.0, *) {
          // ... new content here
      } else {
          // ... old content here
      }
   }
} 

Upvotes: 1

Related Questions