Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Section initializer in SwiftUI

Xcode 12.3 | SwiftUI 2.0 | macOS 11

I'm trying to get a custom initializer to write this:

Section("TITLE") { ... content ... }

Instead of this:

Section(header: Text("TITLE") { ... content ... } 

But I cannot create a custom initializer because self.initis not recognized:

extension Section { 
    init(_ title: String, @escaping () -> Content) { 
        self.init(........ <--- No autocompletions for self initializers
    }
}

I have the same problem with custom inits for Buttons.

Does anyone knows how to access to that initializers from extensions?

like image 254
AlbertUI Avatar asked May 08 '26 17:05

AlbertUI


1 Answers

Here is a solution (tested with Xcode 12.1 / iOS 14.1)

extension Section where Parent == Text, Content: View, Footer == EmptyView {
    init(_ title: String, content: @escaping () -> Content) {
        self.init(header: Text(title), content: content)
    }
}

On GitHub

like image 170
Asperi Avatar answered May 11 '26 05:05

Asperi