1

我正在尝试覆盖的功能,edgesIgnoringSafeArea(_:)因为我在视图中对垂直安全区域进行了自定义处理。但是,没有超级可以调用,所以我不确定除了处理每个安全区域边缘之外如何进行。任何意见,将不胜感激。

// 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)))  
}
4

2 回答 2

1

我们不能覆盖修饰符,但我们可以添加我们自己的使用标准,比如

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

注意:虽然我认为这样没有多大意义overriding,因为只是使用 of.edgesIgnoringSafeArea(.horizontal)更简单、更明显、更易读……但无论如何。

于 2020-12-31T06:24:25.923 回答
0

想出了如何做到这一点。我用它包裹了我的视图AnyView并将选择性安全区域应用到它上面。

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

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

    return AnyView(self).edgesIgnoringSafeArea(edges.subtracting(.vertical))
}
于 2020-12-31T22:59:54.953 回答