gpt4 book ai didi

ios - SwiftUI 按钮比例宽度高度

转载 作者:行者123 更新时间:2023-11-28 23:26:51 25 4
gpt4 key购买 nike

我们如何在 SwiftUI 中为 Button 实现 proportional 大小

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.

最佳答案

您可以使用GeometryReader访问设备sizeframe来设置按钮的比例宽度。例如:

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")
}
}
}
}

关于ios - SwiftUI 按钮比例宽度高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58412568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com