0

How can we achieve proportional size for Button in SwiftUI

struct ContentView: View {

    @State private var emailText = ""
    @State private var passwordText = ""

    var body: some View {
        NavigationView {
            ScrollView {

                VStack(alignment: .center, spacing: 30.0, content: {

                    TextField("Enter Email", text: $emailText)
                        .textFieldStyle(RoundedBorderTextFieldStyle())

                    SecureField("Enter Password", text: $passwordText)
                        .textFieldStyle(RoundedBorderTextFieldStyle())

                    Button(action: {
                        print("Button Tapped")
                    }) {
                        Text("Login")

                    }.frame(width: 100,
                            height: 40,
                            alignment: .center).background(Color.orange)

                   Button(action: {
                     print("Button Tapped")
                   }) {
                     Text("Sign Up")

                  }.frame(width: 150,
                         height: 40,
                         alignment: .center).background(Color.yellow)


                }).padding()
            }
            .navigationBarTitle("Login")
        }
    }
}

enter image description here

How to achieve proportional for Login and Sign Up button according to device wise.

0

1 Answer 1

1

You can use GeometryReader to access device size and frame to set proportional width for Buttons. For example:

struct ContentView: View {

    @State private var emailText = ""
    @State private var passwordText = ""

    var body: some View {
        GeometryReader { geometry in
            NavigationView {
                ScrollView {

                    VStack(alignment: .center, spacing: 30.0, content: {

                        TextField("Enter Email", text: self.$emailText)
                            .textFieldStyle(RoundedBorderTextFieldStyle())

                        SecureField("Enter Password", text: self.$passwordText)
                            .textFieldStyle(RoundedBorderTextFieldStyle())

                        Button(action: {
                            print("Button Tapped")
                        }) {
                            Text("Login")

                        }.frame(width: geometry.size.width / 4,
                                height: 40,
                                alignment: .center).background(Color.orange)

                        Button(action: {
                            print("Button Tapped")
                        }) {
                            Text("Sign Up")

                        }.frame(width: geometry.size.width / 3,
                                height: 40,
                                alignment: .center).background(Color.yellow)


                    }).padding()
                }
                .navigationBarTitle("Login")
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.