maxWidth 无穷大失败 SwiftUI

Fle*_*xer 2 xcode uibutton swift swiftui

我有以下代码:

Button(action: {
                }, label: {
                    Text("Save".uppercased())
                        .foregroundColor(.white)
                        .font(.headline)
                        .background(Color.accentColor)
                        .frame(height: 55)
                        .frame(maxWidth: .infinity)
                        .cornerRadius(10)
                })
            }
            .padding(14)
Run Code Online (Sandbox Code Playgroud)

我已经检查过了,显然遗漏了一些东西,因为最大宽度不起作用。该按钮仍然紧紧限制在“SAVE”文本周围。我也尝试过手动调整宽度,但这并没有改变任何东西。

有什么建议么?我正在运行 XCode 13。

Chr*_*isR 6

顺序在视图修饰符中很重要;)
我想你想要这个:

            Text("Save".uppercased())
                .frame(maxWidth: .infinity)
                .frame(height: 55)
                .background(Color.accentColor)
                .cornerRadius(10)
            
                .foregroundColor(.white)
                .font(.headline)
Run Code Online (Sandbox Code Playgroud)

文本本身的高度和宽度仅满足其需要,因此首先定义尺寸的框架,然后是该区域的背景颜色,然后是角半径。
前景色和字体可以出现在任何地方。

您可以在预览中查看和检查许多(如果有效的话)效果,您可以在其中选择单行代码并查看生成的帧。