SwiftUI how do I use .textFieldStyle(.myTextFieldStyle) instead of textFieldStyle(MyTextFieldStyle())

Josh

Apologies for the title.

I don't know how to word this question properly because I have trouble finding what this is called.

Please allow me to ask this question using examples instead...

Basically I want to turn this:


struct MyTextField: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("Label")
            TextField("", text: .constant(""), axis: .vertical)
                .textFieldStyle(MyTextFieldStyle())
            
        }
        .padding()
    }
}

struct MyTextFieldStyle: TextFieldStyle {
    func _body(configuration: TextField<Self._Label>) -> some View {
        configuration
            .padding()
            .overlay {
                RoundedRectangle(cornerRadius: 5, style: .continuous)
                    .stroke(.secondary, lineWidth: 1)
            }
    }
}

Into this:


struct MyTextField: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("Label")
            TextField("", text: .constant(""), axis: .vertical)
                .textFieldStyle(.myTextFieldStyle)
            
        }
        .padding()
    }
}

struct MyTextFieldStyle: TextFieldStyle {
    func _body(configuration: TextField<Self._Label>) -> some View {
        configuration
            .padding()
            .overlay {
                RoundedRectangle(cornerRadius: 5, style: .continuous)
                    .stroke(.secondary, lineWidth: 1)
            }
    }
}

Note that the only difference is

.textFieldStyle(MyTextFieldStyle())

vs

.textFieldStyle(.myTextFieldStyle)

I thought I could achieve this using an extension to TextField but it didn't seem to work:

extension TextField {
    var caviaTextFieldStyle: CaviaTextFieldStyle {
        .init()
    }
}

----- answered -----

So the solution is:


extension TextFieldStyle where Self == MyTextFieldStyle {
    static var myTextFieldStyle: MyTextFieldStyle {
        .init()
    }
}

user16217248

Try:

extension TextFieldStyle {
    static var myTextFieldStyle {
        return MyTextFieldStyle()
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I use TabbedView in SwiftUI?

How do I use SFSafariViewController with SwiftUI?

How do I use TextInput for WatchOS in swiftUI

How do I use a NavigationLink in SwiftUI?

how do I use current node instead of this?

How do I effectively use navigation links in a simple SwiftUI app?

SwiftUI & Core Data - How do I use a parent record in a predicate?

How do I use a converted jpgData image in SwiftUI?

Use MetalView with SwiftUI? How do I put something to display in there?

How do I make Alien use an existing tarball instead of downloading?

How do I change Eclipse to use spaces instead of tabs?

How do I use reduce function instead of recurring function?

How do I force compiler to use aggregate init instead of constructor

How do I configure Notepad++ to use spaces instead of tabs?

How do I uninstall PIL for python 2.7 and use Pillow instead?

How do I use jQuery Post/Ajax instead of form submit?

How do I use patterns instead of colors in a county map?

How do I use an array instead of the variables in this repetitive function?

How do I use IsError on a function value, instead of cell value?

How do I use Any() instead of RemoveAll() to exclude list items?

How do I use json instead of plist to populate TableView

HOw do I use the index as a reference instead of the date

How do I force the DotNet Framework to use JIT instead of NGEN?

How do I use async pipe instead of using subscribe?

How do I convert this to use a text file instead?

How do I use spring @SpringBootTest instead of DataJPATest

How to get TextFieldStyle cursor in a skin?

Do I have to use an ObservableObject in SwiftUI?

SwiftUI - Color behaves differently if I use a ScrollView instead of a VStack