【问题标题】:SwiftUI button text centeringSwiftUI 按钮文本居中
【发布时间】:2020-11-27 08:31:25
【问题描述】:

我想我有一个相当简单的问题。我希望按钮中的文本居中。这是我所拥有的以及按钮图像的链接。

Text("Button")
    .padding(.leading, 40)
    .padding(.trailing, 40)
    .padding(.top, 20)
    .padding(.trailing, 20)
    .background(Color.blue)
    .foregroundColor(.white)
    .cornerRadius(40)

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    我建议使用 .background 代替固定框架,如下所示:

    Text("Button")
    .frame(width: 100, height: 32)
    .foregroundColor(.white)
    .background(Color.blue)
    .cornerRadius(40)
    

    【讨论】:

      【解决方案2】:

      您指定.padding(.trailing, 40).padding(.trailing, 20)。将最后一个 trailing 更改为 bottom

      尝试以下方法:

      Text("Button")
          .padding(.leading, 40)
          .padding(.trailing, 40)
          .padding(.top, 20)
          .padding(.bottom, 20)
          .background(Color.blue)
          .foregroundColor(.white)
          .cornerRadius(40)
      

      由于您指定了两次trailing,因此似乎添加了值(20 和 40),使尾部填充为 60。 根本没有指定底部填充。

      【讨论】:

        猜你喜欢
        • 2020-02-18
        • 2020-10-20
        • 1970-01-01
        • 1970-01-01
        • 2012-01-02
        • 2012-07-28
        • 1970-01-01
        • 2011-11-23
        • 2017-03-17
        相关资源
        最近更新 更多