DepthAlignmentID
A type that defines a custom depth alignment guide.
Declaration
protocol DepthAlignmentIDOverview
SwiftUI provides guides for the front, center, and back of a view. Conform to this protocol when you need to align views on some other plane along the depth axis, the way AlignmentID lets you add guides across width and height.
Implement defaultValue(in:) to say where the guide falls in a view that does not set it, then wrap the type in a DepthAlignment so containers can align to it:
private enum FrontThird: DepthAlignmentID {
static func defaultValue(in context: ViewDimensions3D) -> CGFloat {
context.size.depth / 3
}
}
extension DepthAlignment {
static let frontThird = DepthAlignment(FrontThird.self)
}A layout then aligns its subviews on the new guide, and any subview can override the default with alignmentGuide(_:computeValue:):
let shelf = HStackLayout().depthAlignment(.frontThird)
shelf {
Model3D(named: "lamp")
Model3D(named: "table")
.alignmentGuide(.frontThird) { context in
context.size.depth / 2
}
}