9

所以我正在尝试创建一个视图,该视图采用 viewBuilder 内容,循环内容的视图并在每个视图和另一个视图之间添加分隔线

struct BoxWithDividerView<Content: View>: View {
    let content: () -> Content
    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }
    var body: some View {
        VStack(alignment: .center, spacing: 0) {
            // here
            
        }
        .background(Color.black)
        .cornerRadius(14)
    }
}

所以在我写“这里”的地方,如果有意义的话,我想循环查看内容的视图。我将编写一个不起作用的代码,但这解释了我想要实现的目标:

ForEach(content.subviews) { view  in
     view
     Divider()
}

怎么做?

4

2 回答 2

3

我刚刚回答了另一个类似的问题,链接在这里。将对链接的答案进行任何改进,因此请先检查那里。

此处的 Swift 包中此(但更高级)的 GitHub 链接

但是,这是具有相同TupleView扩展名但视图代码不同的答案。

用法:

struct ContentView: View {
    
    var body: some View {
        BoxWithDividerView {
            Text("Something 1")
            Text("Something 2")
            Text("Something 3")
            Image(systemName: "circle")  // Different view types work!
        }
    }
}

你的BoxWithDividerView

struct BoxWithDividerView: View {
    let content: [AnyView]
    
    init<Views>(@ViewBuilder content: @escaping () -> TupleView<Views>) {
        self.content = content().getViews
    }
    var body: some View {
        VStack(alignment: .center, spacing: 0) {
            ForEach(content.indices) { index in
                if index != 0 {
                    Divider()
                }
                
                content[index]
            }
        }
//        .background(Color.black)
        .cornerRadius(14)
    }
}

最后是主要的,TupleView扩展:

extension TupleView {
    var getViews: [AnyView] {
        makeArray(from: value)
    }
    
    private struct GenericView {
        let body: Any
        
        var anyView: AnyView? {
            AnyView(_fromValue: body)
        }
    }
    
    private func makeArray<Tuple>(from tuple: Tuple) -> [AnyView] {
        func convert(child: Mirror.Child) -> AnyView? {
            withUnsafeBytes(of: child.value) { ptr -> AnyView? in
                let binded = ptr.bindMemory(to: GenericView.self)
                return binded.first?.anyView
            }
        }
        
        let tupleMirror = Mirror(reflecting: tuple)
        return tupleMirror.children.compactMap(convert)
    }
}

结果:

结果

于 2021-04-24T14:09:44.093 回答
1

所以我最终这样做了

@_functionBuilder
struct UIViewFunctionBuilder {
    static func buildBlock<V: View>(_ view: V) -> some View {
        return view
    }
    static func buildBlock<A: View, B: View>(
        _ viewA: A,
        _ viewB: B
    ) -> some View {
        return TupleView((viewA, Divider(), viewB))
}
}

然后我像这样使用我的函数构建器

struct BoxWithDividerView<Content: View>: View {
    let content: () -> Content
    init(@UIViewFunctionBuilder content: @escaping () -> Content) {
        self.content = content
    }
    var body: some View {
        VStack(spacing: 0.0) {
            content()
        }
        .background(Color(UIColor.AdUp.carbonGrey))
        .cornerRadius(14)
    }
}

但问题是这只适用于最多 2 个表达式视图。我将发布一个单独的问题,说明如何能够将其传递给数组

于 2020-10-08T07:31:04.100 回答