Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Get Coordinates of TextField or any View

Tags:

xcode

ios

swiftui

I'm struggling to find a way in SwiftUI to get the x,y coordinates of the origin of a TextField (or any view). I can certainly provide a position or offset to move the view but I can't seem to find a way to get a reference to the TextField to get its coordinates.

In UIKit, I believe I would use myTextField.frame.origin.x

enter image description here

This is a very simple example:

struct ContentView: View {

    @State private var someNumber1 = "1000"
    @State private var someNumber2 = "2000"
    //bunch more

    @State private var enteredNumber = "Some Text at the Top"
    @State var value: CGFloat = 0

    var body: some View {
        ScrollView {
            VStack {
                Spacer()
                Text("\(enteredNumber)")
                Spacer()

                Group { //1
                    TextField("Placeholder", text: $someNumber1)
                        .keyboardType(.default)
                        .textFieldStyle(RoundedBorderTextFieldStyle())

                        //this does not work
                        .onTapGesture {
                            let aY = (textfieldreferencehere).frame.origin.y
                        }

                    TextField("Placeholder", text: $someNumber2)
                        .keyboardType(.default)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
               }//group 1     

               //bunch more

                Button(action: {
                    self.enteredNumber = self.someNumber1
                    self.someNumber1 = ""
                    UIApplication.shared.endEditing()
                }) {
                    Text("Submit")
                }
                .padding(.bottom, 50)

            }//outer v
                .padding(.horizontal, 16)
                .padding(.top, 44)

        }//Scrollview or Form
        .modifier(AdaptsToSoftwareKeyboard())
    }
}

Any Guidance would be appreciated. Xcode 11.4.1

like image 217
JohnSF Avatar asked Apr 22 '20 00:04

JohnSF


1 Answers

Here is a demo of how specific view coordinates can be read (see helpful comments inline)

struct DemoReadViewOrigin: View {

    @State private var someNumber1 = "1000"
    @State private var someNumber2 = "2000"
    //bunch more

    @State private var enteredNumber = "Some Text at the Top"
    @State var value: CGFloat = 0

    var body: some View {
        ScrollView {
            VStack {
                Spacer()
                Text("\(enteredNumber)")
                Spacer()

                Group { //1
                    TextField("Placeholder", text: $someNumber1)
                        .keyboardType(.default)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .background(GeometryReader { gp -> Color in
                            let rect = gp.frame(in: .named("OuterV")) // < in specific container
                            // let rect = gp.frame(in: .global) // < in window
                            // let rect = gp.frame(in: .local) // < own bounds
                            print("Origin: \(rect.origin)")
                            return Color.clear
                        })

                        //this does not work
                        .onTapGesture {
                        }

                    TextField("Placeholder", text: $someNumber2)
                        .keyboardType(.default)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
               }//group 1

               //bunch more

                Button(action: {
                    self.enteredNumber = self.someNumber1
                    self.someNumber1 = ""
//                    UIApplication.shared.endEditing()
                }) {
                    Text("Submit")
                }
                .padding(.bottom, 50)

            }//outer v
                .coordinateSpace(name: "OuterV") // << declare coord space
                .padding(.horizontal, 16)
                .padding(.top, 44)

        }//Scrollview or Form
//        .modifier(AdaptsToSoftwareKeyboard())
    }
}

backup

like image 101
Asperi Avatar answered Oct 12 '22 23:10

Asperi