Reputation: 3727
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")
}
}
}
How to achieve proportional for Login and Sign Up button according to device wise.
Upvotes: 0
Views: 1408
Reputation: 2866
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")
}
}
}
}
Upvotes: 1