【问题标题】:How to extend the width of a button using SwiftUI如何使用 SwiftUI 扩展按钮的宽度
【发布时间】:2019-10-18 21:04:45
【问题描述】:

我不知道如何在 SwiftUI 中更改按钮的宽度。

我已经尝试过: 使用 .frame(minWidth: 0, maxWidth: .infinity), 在按钮和导航链接周围使用 Spacer(), 使用文本字段上的框架和按钮上的填充,查看文档以及我在网上搜索时发现的其他一些内容。然而,没有任何改变按钮的宽度。

NavigationLink(destination: Home(), isActive: self.$isActive) { Text("") }
Button(action: { self.isActive = true }) { LoginBtn() }

struct LoginBtn: View {
    var body: some View {
        Text("Login")
            .fontWeight(.bold)
            .padding()
            .foregroundColor(Color.white)
            .background(Color.orange)
            .cornerRadius(5.0)
    }
}

Photo of current button

我希望按钮的扩展与所使用的 TextField 的宽度相似。同样,我知道已经发布了答案,但由于某种原因,我无法让我的工作。谢谢!

【问题讨论】:

    标签: ios swiftui xcode11


    【解决方案1】:

    声明你自己的按钮样式:

    struct WideOrangeButton: ButtonStyle {
    
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
                .padding()
                .frame(minWidth: 0,
                       maxWidth: .infinity)
                .foregroundColor(.white)
                .padding()
                .background( RoundedRectangle(cornerRadius: 5.0).fill(Color.orange)
            )
        }
    }
    

    然后像这样使用它:

    Button(action: { self.isActive = true }) {
            Text("Login")
               .fontWeight(.bold)
        }   .buttonStyle(WideOrangeButton())
    

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 2019-12-01
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多