I would like to connect two SwiftUI Views with an arrow, one pointing to the other. For example, in this View:
struct ProfileIconWithPointer: View {
var body: some View {
VStack {
Image(systemName: "person.circle.fill")
.padding()
.font(.title)
Text("You")
.offset(x: -20)
}
}
}
I have a Text
view and an Image
view. I would like to have an arrow pointing from one to the other, like this:
However, all the solutions that I could find rely on knowing the position of each element apriori, which I’m not sure SwiftUI’s declarative syntax allows for.
2
Answers
you can use the following code to display an arrow.
you can use this in your code as follows.
This will looks like as follows.
Depend on your scenario modify the code. Hope this will helps you.
This was a timely question for me as I was recently asked to do something similar. I embellished udi’s answer with Geometry Reader which allows assigning the positions(center point) of the views and the arrow’s start and end points. This idea might be helpful for some scenarios.
The result looking like this and adjusting as position1 and position2 are changed: