SwiftUI 按钮文本对齐问题
SwiftUI Button text alignment issue
我想UI在手表应用中这样
但是信息按钮文本对齐方式不正确。见下图:
尝试了文本“i”和系统图像 'info',仍然是同样的问题
这是我的代码:
VStack {
TextField("User Name", text: $userName)
.textContentType(.username)
.multilineTextAlignment(.center)
SecureField("Password", text: $password)
.textContentType(.password)
.multilineTextAlignment(.center)
Spacer()
Spacer()
HStack {
Button(action: {
//action
}) {
Text("Go")
}.disabled(userName.isEmpty || password.isEmpty)
.frame(width: 80, height: 40, alignment: Alignment.bottom)
Spacer()
VStack {
Spacer()
Button(action: {
//action
}) {
Image(systemName: "info")
}.frame(width: 25, height: 25, alignment: Alignment.bottom)
.foregroundColor(.black)
.background(Color.white)
.clipShape(Circle())
}
}
}
您只是不需要那些框架对齐和间隔符,通过删除它们让默认的中心对齐生效:
HStack(alignment: .bottom) { // << here !!
Button(action: {
//action
}) {
Text("Go")
}.disabled(userName.isEmpty || password.isEmpty)
.frame(width: 80, height: 40)
Spacer()
Button(action: {
//action
}) {
Image(systemName: "info")
}.frame(width: 25, height: 25)
.foregroundColor(.black)
.background(Color.white)
.clipShape(Circle())
}
我想UI在手表应用中这样
但是信息按钮文本对齐方式不正确。见下图: 尝试了文本“i”和系统图像 'info',仍然是同样的问题
这是我的代码:
VStack {
TextField("User Name", text: $userName)
.textContentType(.username)
.multilineTextAlignment(.center)
SecureField("Password", text: $password)
.textContentType(.password)
.multilineTextAlignment(.center)
Spacer()
Spacer()
HStack {
Button(action: {
//action
}) {
Text("Go")
}.disabled(userName.isEmpty || password.isEmpty)
.frame(width: 80, height: 40, alignment: Alignment.bottom)
Spacer()
VStack {
Spacer()
Button(action: {
//action
}) {
Image(systemName: "info")
}.frame(width: 25, height: 25, alignment: Alignment.bottom)
.foregroundColor(.black)
.background(Color.white)
.clipShape(Circle())
}
}
}
您只是不需要那些框架对齐和间隔符,通过删除它们让默认的中心对齐生效:
HStack(alignment: .bottom) { // << here !!
Button(action: {
//action
}) {
Text("Go")
}.disabled(userName.isEmpty || password.isEmpty)
.frame(width: 80, height: 40)
Spacer()
Button(action: {
//action
}) {
Image(systemName: "info")
}.frame(width: 25, height: 25)
.foregroundColor(.black)
.background(Color.white)
.clipShape(Circle())
}