gpt4 book ai didi

swift - 使 Button 跨越 VStack

转载 作者:可可西里 更新时间:2023-11-01 00:19:01 29 4
gpt4 key购买 nike

我目前有以下 SwiftUI View :

HStack {
...
VStack {
TextField { ... }
SecureField { ... }
Button { ... }
}
...
}

enter image description here

我添加了一个 .background(Color.green)Button ,如您所见, View 与文本非常贴合。

我想知道是否有办法调整按钮的宽度,使其填满 VStack - 类似于 .fill UIStackView 的模式.

最佳答案

最好的方法是通过 .frame(maxWidth: .infinity) https://developer.apple.com/documentation/swiftui/view-layout

如果你想让按钮不居中,你需要指定alignment
例如:.frame(maxWidth: .infinity, alignment: .leading)

Button(action: handleSignInAction) {
Text("Sign In")
}
.frame(maxWidth: .infinity)
.background(Color.green)

2019 年的旧答案:

您可以使用带有 TextSpacerHStack 来获得填充宽度的 Button它的父级:

Button(action: handleSignInAction) {
HStack {
Spacer()
Text("Sign In")
Spacer()
}
}.background(Color.green)

关于swift - 使 Button 跨越 VStack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56471004/

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