oaranger
oaranger

Reputation: 189

Switch TextFieldStyle between OS

Say I have 2 TextFieldStyles like below where one of them should run with iOS 15+ if available.

struct TF1: TextFieldStyle {
    init()
    func _body(configuration: TextField<Self._Label>) -> some View {}
}
@available(iOS 15.0, *)
struct TF2: TextFieldStyle {
    @FocusState var focused: Bool
    
    init()
    func _body(configuration: TextField<Self._Label>) -> some View {}
}

Now I want to use theses style with modifier .textFieldStyle() where I pass in the right style.

For example

if #available(iOS 15.0, *) {
    TextField("").textFieldStyle(TF2())
} else {
    TextField("").textFieldStyle(TF1())
}

Above code works but can be improved. Is there a way to write an adapter so I can just do something like below?

   TextField("").textFieldStyle(Adapter())

Upvotes: 0

Views: 63

Answers (0)

Related Questions