我可以在SwiftUI 中布局后获得`View` 的位置吗? [英] can I get the position of a `View` after layout in SwiftUI?
本文介绍了我可以在SwiftUI 中布局后获得`View` 的位置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在布局后获得 View
的框架?我想在布局定位后绘制一条连接两个视图的线:
Is there a way to get a View
's frame after layout? I'd like to draw a line connecting two views after layout has positioned them:
看来我需要类似 在 React 中测量
.
It seems I need something like measure
in React.
推荐答案
使用 GeometryReader
获取每个视图的框架,并使用框架确定两个视图之间路径的点.
Use a GeometryReader
to get the frame of each view and use the frame to determine the points for a path between the two views.
struct ContentView : View {
var body: some View {
GeometryReader { geometry -> Text in
let frame = geometry.frame(in: CoordinateSpace.local)
return Text("\(frame.origin.x), \(frame.origin.y), \(frame.size.width), \(frame.size.height)")
}
}
}
这篇关于我可以在SwiftUI 中布局后获得`View` 的位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文