I'm trying to combine the View1 and View2 using a @viewBuilder named SeparateViewPanels. But it throws the following error. Why is that? I can see that the error is about a wrong type I used. As I see it basically says I can't use the type of view when it expects a type of content.
Error: Cannot convert return expression of type 'TupleView<(View1, View2)>' to return type '(toolBar: Content, view2D: Content)'
but even if I replace the content with anyView etc, it still throws more errors. So it was not the solution. How to solve this? I'm trying out the @viewbuilder functionality so I can't completely eliminate @viewbuilder
import AppKit
import Combine
import SwiftUI
@main
struct demo44App: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct View1: View {
var body: some View {
Text("Hello, World!")
}
}
struct View2: View {
var body: some View {
Text("Hello, World!")
}
}
struct ContentView: View {
var body: some View {
SeparateViewPanels {
View1()
View2()
}
}
}
struct SeparateViewPanels<Content: View>: View {
@State var propertiesWindowWidth: Double = 100
let contents: () -> (toolBar: Content, view2D: Content)
init(@ViewBuilder contents: @escaping () -> (toolBar: Content, view2D: Content)) {
self.contents = contents
}
var body: some View {
GeometryReader { geo in
VStack(alignment: .center) {
HStack (alignment: .center, spacing: 0) {
Rectangle()
.fill(.blue)
.opacity(0.7)
.frame(width: 100, height: geo.size.height)
.overlay {
contents().toolBar
}
Rectangle()
.fill(.gray)
.opacity(0.7)
.frame(width: 100, height: geo.size.height)
.overlay {
contents().view2D
}
}
}
}
}
}
toolBarandview2Dare views of different types, soSeparateViewPanelsshould have 2 type parameters -ToolbarandView2D. Then, change the return type of thecontentclosure toTupleView<(Toolbar, View2D)>as the error message suggests.You can get the separate views from a
TupleViewby using thevalue.0andview.1properties.Also consider having separate initialiser parameters for the two views. I think it is clearer for the user of
SeparateViewPanelsto see that you expect exactly two views.The caller would do: