I am currently working on my project for the Swift Student Challenge 2024. I am trying to implement a feature (.defaultScrollAnchor on a ScrollView in this instance). However, when trying to build/run the app in Xcode it says that .defaultScrollAnchor is only compatible with iOS 17. If I try to use .scrollPosition(initialAnchor: ), it also doesn't work (likely because it has been depreciated). I printed the system version as suggested in another post, and it showed the simulator was running iOS 17.2. Why can't the app build/run with this feature if the simulator is running iOS 17.2?
MacBook Pro 14" 2021 M1 Pro
Xcode Version 15.1
macOS Sonoma 14.0
Hi! It seems that App Playgrounds require support for iOS 16.0 on this version of Xcode (not sure if this is a bug or intended for compatibility reasons). Even though you want to run your app on an iOS 17.0 device, the App Playground is set to build for iOS 16 devices too, and this is why your build fails (remember, to run a code it must first be built, and building requires your code to work on all devices it is configured for, not just on the one you use to test it).
You should've received some relevant error messages and fixes that mention the if #available
version check or the @available(iOS 17.0, *)
attribute. These may be useful If you want to maintain the iOS 16 compatibility. This is analogous to changing the minimum iOS deployment version in an .xcodeproj
.
You could use the if #available
version check like this:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
if #available(iOS 17.0, *) {
ScrollView {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("iOS 17.0")
}
.defaultScrollAnchor(.bottom)
} else {
ScrollView {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("iOS 16.0")
}
}
}
}
}
Or you could add the @available(iOS 17.0, *)
attribute to the enclosing View like this:
import SwiftUI
@available(iOS 17.0, *)
struct ContentView: View {
var body: some View {
VStack {
ScrollView {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.defaultScrollAnchor(.bottom)
}
}
}
And then, in the App struct, use the if #available
version check:
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
if #available(iOS 17.0, *) {
ContentView()
} else {
// Fallback on earlier versions
Text("This feature requires iOS 17.0.")
}
}
}
}
Now, if you want a "hacky" method, you could edit the package files of your App Playground. To do this, go to File > Show in Finder, and then open the Package.swift
file. After that, you can change the .iOS("16.0")
line to 17.0.
Also, I recommend you to use the latest Xcode version & macOS (they bring bug fixes and security improvements 😁).
Good luck and Happy New Year!