@Claude31. I got the same problem (Xcode12)
ContentView.swift:
Code Block swiftimport SwiftUI |
|
struct ContentView: View { |
@State var isSideBarPresented = true |
@State var node = Node3D() |
|
var body: some View { |
VStack { |
Button("Toggle") { isSideBarPresented.toggle() } |
HSplitView { |
Color.white.frame( |
minWidth: 100, |
maxWidth: .infinity, |
minHeight: 0, |
maxHeight: .infinity |
) |
|
if isSideBarPresented { |
Node3DConfigurationView(node: $node) |
} |
} |
}.frame( |
minWidth: 0, |
maxWidth: .infinity, |
minHeight: 0, |
maxHeight: .infinity |
) |
} |
} |
Node3DConfigurationView.swift:
Code Block swiftimport simd |
import SwiftUI |
|
struct Node3D { |
var pivot: simd_float3 = .zero |
var position: simd_float3 = .zero |
var rotation: simd_float3 = .zero |
var scale: simd_float3 = .zero |
} |
|
/* not sure if needed, but I randomly got some error without it */ |
fileprivate func wrap(_ binding: Binding<Float>) -> Binding<NSNumber> { |
Binding( |
get: { NSNumber(value: binding.wrappedValue) }, |
set: { binding.wrappedValue = $0.floatValue } |
) |
} |
|
struct Node3DConfigurationView: View { |
@Binding var node: Node3D |
var body: some View { |
ScrollView(.vertical) { |
Group { |
makeSection("Pivot", value: $node.pivot) |
makeSection("Position", value: $node.position) |
makeSection("Rotation", value: $node.rotation) |
makeSection("Scale", value: $node.scale) |
} |
.textFieldStyle(RoundedBorderTextFieldStyle()) |
} |
} |
|
func makeSection(_ headerTitle: String, value: Binding<simd_float3>) -> some View { |
Section(header: makeHeader(headerTitle)) { |
HStack { |
HStack { |
Text("x") |
TextField("\(headerTitle)X", value: wrap(value.x), formatter: NumberFormatter()) |
} |
.frame(minWidth: 50) |
HStack { |
Text("y") |
TextField("\(headerTitle)Y", value: wrap(value.y), formatter: NumberFormatter()) |
} |
.frame(minWidth: 50) |
HStack { |
Text("z") |
TextField("\(headerTitle)Z", value: wrap(value.z), formatter: NumberFormatter()) |
} |
.frame(minWidth: 50) |
}.padding() |
} |
} |
|
func makeHeader(_ title: String) -> some View { |
Text(title).frame(maxWidth: .infinity, alignment: .leading) |
} |
} |
AppDelegate.swift (Default, generated by Xcode):
Code Block swiftimport Cocoa |
import SwiftUI |
|
@NSApplicationMain |
class AppDelegate: NSObject, NSApplicationDelegate { |
var window: NSWindow! |
|
func applicationDidFinishLaunching(_ aNotification: Notification) { |
let contentView = ContentView() |
window = NSWindow( |
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), |
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], |
backing: .buffered, defer: false |
) |
window.isReleasedWhenClosed = false |
window.center() |
window.setFrameAutosaveName("Main Window") |
window.contentView = NSHostingView(rootView: contentView) |
window.makeKeyAndOrderFront(nil) |
} |
} |