'SwiftUI - proper use of @available() and #available
Am I missing something in the following code?
This app builds (macOS Monterey b5 / Xcode 13 b5) and executes perfectly on an iOS 15 device -- but causes a SIGABRT on an iOS 14.7 device...
import SwiftUI
struct ContentView: View {
@State private var text = "This app causes a runtime error on ios 14.7"
@available(iOS 15.0, *)
@FocusState var isInputActive: Bool
var body: some View {
if #available(iOS 15.0, *) {
TextEditor(text: $text)
.focused($isInputActive)
} else { // ios14 or <
TextEditor(text: $text)
}
}
}
Solution 1:[1]
Was faced same issue and found solution by keeping property values in view modifier.
Here I give simple example :
@available(iOS 15.0, *)
struct TextEditor: ViewModifier {
func body(content: Content) -> some View {
content
.font(.largeTitle)
.foregroundColor(.white)
.padding()
.background(.blue)
}
}
Then call as below :
if #available(iOS 15.0, *){
Text("Hello world").modifier(TextEditor())
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |