有没有办法在 SwiftUI 中获取子视图的大小?
我基本上希望做 UIKit 相当于:
self.child.frame.origin.x -= self.child.intrinsicContentSize.width/2.0
Run Code Online (Sandbox Code Playgroud)
我认为 GeometryReader 不会工作,因为它返回父级中的可用大小。
[编辑] 我发现可以使用.alignmentGuide(_, computeValue:)虽然这绝对是一个黑客来获取和保存尺寸。
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())
Run Code Online (Sandbox Code Playgroud)
swiftui ×1