Jer*_*hur 8 ios swiftui xcode11
我不知道如何更改 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)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望按钮能够扩展为与所使用的文本字段的宽度相似。再说一遍,我知道已经发布了答案,但由于某种原因我无法让我的答案工作。谢谢!
LuL*_*aGa 11
声明您自己的按钮样式:
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)
)
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
Button(action: { self.isActive = true }) {
Text("Login")
.fontWeight(.bold)
} .buttonStyle(WideOrangeButton())
Run Code Online (Sandbox Code Playgroud)
Rya*_*yan 10
我喜欢这种方法,因为它允许我使用默认的按钮样式,但仍然会产生更宽的按钮。
Button(action: {
// Whatever button action you want.
}, label: {
Text("Okay")
.frame(maxWidth: .infinity)
})
.buttonStyle(.automatic)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4225 次 |
| 最近记录: |