Is there any way to get the size of a child view in SwiftUI?
I'm basically looking to do the UIKit equivalent of:
self.child.frame.origin.x -= self.child.intrinsicContentSize.width/2.0
I don't think a GeometryReader would work since that returns the available size in the parent.
[Edit] I've found it's possible to get and save the dimensions using .alignmentGuide(_, computeValue:)
though that's definitely a hack.
LessonSliderText(text: self.textForProgress(self.progress), color: self.completedColor)
.alignmentGuide(HorizontalAlignment.leading) { (dimensions) -> Length in
self.textSize = CGSize(width: dimensions.width, height: dimensions.height)
return 0
}
.offset(x: self.width*self.currentPercentage - self.textSize.width / 2.0)
.offset(y: -self.textSize.height/2.0)
.animation(nil)
.opacity(self.isDragging ? 1.0 : 0.0)
.animation(.basic())
You can use AnchorPreferences to bubble information about a child view's geometry up to a parent view. See: https://swiftui-lab.com/communicating-with-the-view-tree-part-2/
Basically, the answer at this point is to use a GeometryReader inside of the child's
background(...)
modifier.Updated and generalized @arsenius code. Now you can easily bind a parent view's state variable.
Usage: