【发布时间】:2022-01-13 19:27:10
【问题描述】:
我正在尝试创建 2 个大小相同的按钮,但大小由按钮内的文本决定。两个按钮分别是“登录”和“创建账号”,所以“创建账号”按钮比较大。我尝试使用 .frame() 来调整大小,但这只是让它周围有额外的填充。
这是我的代码:
HStack {
Button(action: {
print("Login button pressed")
}) {
Text("Login")
.padding()
.foregroundColor(Color.white)
.background(Color(UIColor.buttonColor))
.cornerRadius(15)
}
Button(action: {
print("Create Account button pressed")
}) {
Text("Create Account")
.padding()
.foregroundColor(Color.white)
.background(Color(UIColor.buttonColor))
.cornerRadius(15)
}
}
这就是显示的内容
【问题讨论】:
-
这是否回答了您的问题stackoverflow.com/a/67454262/12299030?当然有最简单的方法 - 对较小的按钮使用相同的标签,透明颜色和所需标签的覆盖。
-
HStack { Button { } label: { Text("Login") } .frame(width: 146.0, height: 36.0) .background(Color(uiColor: UIColor.buttonColor)) .cornerRadius(15.0 ) .foregroundColor(Color.white) Button { } label: { Text("Create Account") } .frame(width: 146.0, height: 36.0) .background(Color(uiColor: UIColor.buttonColor)) .cornerRadius(15.0. 0) .foregroundColor(Color.white) }