SwiftUI按钮文本对齐问题

问题描述

我想这样在手表应用中制作用户界面

enter image description here

但是信息按钮的文本对齐方式不合适。见下图: 尝试同时使用文本“ i”和系统映像“ info”,仍然是同一问题

enter image description here

这是我的代码:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...