swiftuiswiftui-list

SwiftUI equivalent of UITableView.cellLayoutMarginsFollowReadableWidth


What is the SwiftUI equivalent of UIKit's UITableView.cellLayoutMarginsFollowReadableWidth?


Solution

  • Solved my question by creating a ListReadableWidth view that wraps around a regular List.

    struct ListReadableWidth<Content: View>: View {
    
        var content: () -> Content
        var desiredWidth: CGFloat
    
        init(desiredWidth: CGFloat = 685, @ViewBuilder _ content: @escaping () -> Content) {
            self.desiredWidth = desiredWidth
            self.content = content
        }
    
        var body: some View {
            HStack {
                if UIDevice.current.userInterfaceIdiom == .pad {
                    Spacer()
                }
                content()
                    .frame(idealWidth: desiredWidth, maxWidth: desiredWidth)
                if UIDevice.current.userInterfaceIdiom == .pad {
                    Spacer()
                }
            }
            .background(Color(.systemGroupedBackground))
        }
    
    }
    
    #Preview {
        ListReadableWidth {
            List {
                Text("Hi")
            }
        }
    }