How to create custom ListStyle in SwiftUI?

Imt*_*ath 5 ios swift swiftui

I want to make changes to the listItem's background, rowInsets based on multiple conditions. How can I create a struct conforming to the ListStyle protocol?

I tried conforming to it and get the following code blocks. But I've no idea how to proceed from here.


struct CustomListStyle: ListStyle {
    
    static func _makeView<SelectionValue>(value: _GraphValue<_ListValue<CustomListStyle, SelectionValue>>, inputs: _ViewInputs) -> _ViewOutputs where SelectionValue : Hashable {
        
    }
    
    static func _makeViewList<SelectionValue>(value: _GraphValue<_ListValue<CustomListStyle, SelectionValue>>, inputs: _ViewListInputs) -> _ViewListOutputs where SelectionValue : Hashable {
        
    }
}

Run Code Online (Sandbox Code Playgroud)

小智 1

我这几天也面临这个问题。经过一番调查,我最终意识到 SwiftUI 并没有真正“允许”我们通过重写ListStyle协议来自定义列表样式。我发现没有任何与此相关的文档。

然而,实际上可以(不完美,但可行)通过使用 Introspect 来自定义它。请在此处查看更多信息:https://github.com/siteline/SwiftUI-Introspect#list