在 iOS 上匹配父母的宽度有效,但在 Mac OS (SwiftUI) 上不起作用
问题描述:
我正在尝试在 HStack 中拉伸视图.问题是在 iOS 上运行的相同代码在 Mac OS 上不起作用,我找不到任何合乎逻辑的原因.我错过了什么吗?
I'm trying to stretch a view inside a HStack. The problem is the same code that works on iOS does not work on Mac OS and I can't find any logical reason. Am I missing something?
HStack(spacing: 0) {
Button("A") {
}
Button(action: {}) {
Text("HELLO")
.frame(maxWidth: .infinity)
.padding()
.background(Color.red)
}
Button("B") {
}
}.padding()
Mac os 输出不拉伸按钮
在 iOS 上运行的相同代码
答
使用 .buttonStyle(PlainButtonStyle())
和 fixedSize()
(optioanl fixedSize())
Use .buttonStyle(PlainButtonStyle())
and fixedSize()
(optioanl fixedSize())
Button(action: {}) {
Text("HELLO")
.frame(maxWidth: .infinity)
.padding()
.background(Color.red)
.fixedSize()
}
.buttonStyle(PlainButtonStyle())