问题描述
我不知道如何在 SwiftUI 中更改按钮的宽度.
I cannot figure out how to change the width of buttons in SwiftUI.
我已经尝试过:使用 .frame(minWidth: 0, maxWidth: .infinity),在按钮和导航链接周围使用 Spacer(),在文本字段上使用框架并在按钮上使用填充,查看文档以及我在网上搜索时发现的其他一些内容.然而,没有任何改变按钮的宽度.
I have already attempted:using .frame(minWidth: 0, maxWidth: .infinity),using Spacer() around the button and navigationlink,using frame on the Text field and padding on the button, look through the documentation, as well as a few other things I found while just searching online. However nothing changes the buttons width whatsoever.
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)
}
}
我想让按钮扩展到与使用的 TextFields 的宽度相似.同样,我知道已经发布了答案,但由于某种原因我无法让我的工作.谢谢!
I would like to have the button to extend to be similar to the width of the TextFields used. Again, I know there have been answers posted but for some reason I cannot get mine to work. Thanks!
推荐答案
声明你自己的按钮样式:
Declare your own button style:
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())
这篇关于如何使用 SwiftUI 扩展按钮的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!