Ios 快捷按钮比例宽度高度
如何在Ios 快捷按钮比例宽度高度,ios,button,swiftui,Ios,Button,Swiftui,如何在SwiftUI struct ContentView: View { @State private var emailText = "" @State private var passwordText = "" var body: some View { NavigationView { ScrollView { VStack(alignment: .center, spacing: 30
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")
}
}
}
如何根据设备情况实现按比例登录和注册按钮
您可以使用
GeometryReader
访问设备size
和frame
来设置按钮的比例宽度。例如:
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")
}
}
}
}