LayoutKit
LayoutKit copied to clipboard
Ability to translate the position of a view after layout phase
Ability to apply a traslation to a view position after the layout phase by absolute value or by percentage on the container size.
This could be handy in cases where we need to tweak the position of a element in a complex layout without have to create a custom Layout.
I think this would be easily accomplished by creating a custom layout (possible name OffsetLayout
).
Something like this (incomplete implementation)
public class OffsetLayout: Layout {
public let offset: CGPoint
public let sublayout: Layout
public init(offset: CGPoint, sublayout: Layout, /* other standard parameters */) {
self.offset = offset
self.sublayout = sublayout
}
public func measurement(within maxSize: CGSize) -> LayoutMeasurement {
let measurement = sublayout.measure(within: maxSize)
return LayoutMeasurement(layout: self, size: measurement.size, maxSize: maxSize, sublayouts: [sublayout])
}
public func arrangement(within rect: CGRect, measurement: LayoutMeasurement) -> LayoutArrangement {
let offsetRect = CGRectOffset(rect, offset.x, offset.y)
let position = sublayout.arrangement(within: offsetRect, measurement: measurement)
return LayoutArrangement(layout: self, frame: rect, sublayouts: [offsetRect])
}
}
This would probably be appropriate to add as a base layout in LayoutKit.
Do you have an example of a layout where a percentage offset would be useful?