0

How to get the position of green rectangle and set it to POSX & POSY? I'm trying to reposition it when application loads, and when I click a button the text box should move to the position of the rectangle.

import SwiftUI
       class EnvironmentVariables: ObservableObject {
            @Published var POSX: CGFloat = 0.0
            @Published var POSY: CGFloat = 0.0
        }
       struct ContentView: View {
               var body: some View {
                    
                    ZStack {
                   Rectangle ()
                        .frame(width: 5, height: 5)
                        .zIndex(2)
                        .foregroundColor(.green)
                    } .frame(width: 200, height: 200, alignment: .center)
                }
           
        }
         struct docker: View {
            
            @EnvironmentObject var environmentVariables: EnvironmentVariables
            //@StateObject  var environmentVariables = EnvironmentVariables()
            
            
            var body: some View {
            VStack {
                
                Text("The text")
                    .background(.green)
                    .position(x: environmentVariables.POSX, y: environmentVariables.POSX)
            }
            .environmentObject(environmentVariables)
            }
            
        }
TheDummy
  • 65
  • 7
  • This should be helpful https://stackoverflow.com/a/62588295/12299030, and this one https://stackoverflow.com/a/62023131/12299030. – Asperi Feb 10 '22 at 12:45
  • @Asperi I still don't fully get it. Your example has `.overlayPreferenceValue(BoundsPreferenceKey.self) { preferences in` that is applicable if it's in the same view. I have two views above, how to use Geometry without using `.overlayPreferenceValue`? – TheDummy Feb 14 '22 at 13:33

0 Answers0