Saturday, August 3, 2024
HomeiOS Developmentios - Tips on how to take away the road separators from...

ios – Tips on how to take away the road separators from a Listing in SwiftUI with out utilizing ForEach?


IOS 14

enter image description here

There may be at present no resolution to cover the separators on the IOS 14 beta.

When you do not want an editable Listing, it’s best to use a LazyVStack inside a ScrollView.

However if you wish to keep on the Listing. I discovered an answer on the Apple discussion board by samwarner. https://developer.apple.com/boards/thread/651028

It is a non permanent resolution. In some instances chances are you’ll want to regulate the insets.
Right here is its implementation:

struct HideRowSeparatorModifier: ViewModifier {
    static let defaultListRowHeight: CGFloat = 44
    var insets: EdgeInsets
    var background: Coloration
    
    init(insets: EdgeInsets, background: Coloration) {
        self.insets = insets
        var alpha: CGFloat = 0
        UIColor(background).getWhite(nil, alpha: &alpha)
        assert(alpha == 1, "Setting background to a non-opaque colour will end in separators remaining seen.")
        self.background = background
    }
    
    func physique(content material: Content material) -> some View {
        content material
            .padding(insets)
            .body(
                minWidth: 0, maxWidth: .infinity,
                minHeight: Self.defaultListRowHeight,
                alignment: .main
            )
            .listRowInsets(EdgeInsets())
            .background(background)
    }
}

extension EdgeInsets {
    static let defaultListRowInsets = Self(high: 0, main: 16, backside: 0, trailing: 16)
}

extension View {
    func hideRowSeparator(insets: EdgeInsets = .defaultListRowInsets, background: Coloration = .white) -> some View {
        modifier(HideRowSeparatorModifier(insets: insets, background: background))
    }
}

Lastly, right here is the implementation on an inventory. You must add .hideRowSeparator() on the listing cell.

struct CustomRow: View {
    let textual content: String
    
    var physique: some View {
        HStack {
            Textual content(self.textual content)
            Picture(systemName: "star")
        }
    }
}

struct ContentView : View {
    @State personal var fruits = ["Apple", "Orange", "Pear", "Lemon"]
    
    var physique: some View {
        VStack(alignment: .main) {
            Listing {
                ForEach(self.fruits, id: .self) { str in
                    CustomRow(textual content: str)
                        .hideRowSeparator()
                }
            }
        }
        .padding(.high)
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments