robhasacamera
robhasacamera

Reputation: 3336

How to override edgesIgnoringSafeArea (or any View modifier)

I'm trying to override the functionality of edgesIgnoringSafeArea(_:) since I have custom handling for the vertical safe area in my view. However, there's no super to call so I'm not sure how to proceed other then handling every safe area edge. Any advice would be appreciated.

// Will use later for custom handling of vertical safe area
@State private var edgesThatIgnoreSafeArea: Edge.Set = []

func edgesIgnoringSafeArea(_ edges: Edge.Set) -> some View {
    edgesThatIgnoreSafeArea = edges

    // There is no super here to call, and calling self would just call this function again
    return AnyView(edgesIgnoringSafeArea(edges.subtracting(.vertical)))  
}

Upvotes: 1

Views: 315

Answers (2)

robhasacamera
robhasacamera

Reputation: 3336

Figured out how to do this. I wrapped my view with AnyView and applied the selective safe area to it.

@State private var edgesThatIgnoreSafeArea: Edge.Set = []

func edgesIgnoringSafeArea(_ edges: Edge.Set) -> some View {
    edgesThatIgnoreSafeArea = edges

    return AnyView(self).edgesIgnoringSafeArea(edges.subtracting(.vertical))
}

Upvotes: 0

Asperi
Asperi

Reputation: 258117

We cannot override modifiers, but we can add our own which use standard, like

extension View {
    func edgesIgnoringHorizontal(_ edges: Edge.Set) -> some View {
         self.edgesIgnoringSafeArea(edges.subtracting(.vertical))
    }
}

Note: although I don't see much sense in such overriding, because just using of .edgesIgnoringSafeArea(.horizontal) is simpler and more obvious and readable... but anyway.

Upvotes: 1

Related Questions