MacOS上Swiftui的按钮的基线对准

发布于 2025-01-28 23:55:47 字数 1301 浏览 5 评论 0原文

我正在尝试垂直对齐文本和一个常规按钮沿MacOS上的Swiftui的基线。我基本上想从WiFi偏好窗格中复制这种布局:

”

我希望将上述控件放入hstack,并将对齐设置为。 FirstTextBaseline将产生此布局。尽管许多其他控件都是这种情况,但它似乎与非平台按钮不起作用。请参见以下示例:

HStack(alignment: .firstTextBaseline) {
    Text("Text")
    
    Text("Large Text")
        .font(.title)
    
    Button {} label: {
        Text("Button")
    }
    
    Button {} label: {
        Text("Borderless")
    }
    .buttonStyle(.borderless)
    
    Button {} label: {
        Text("Plain")
    }
    .buttonStyle(.plain)
    
    Toggle("Toggle", isOn: .constant(true))
    
    Picker("Picker", selection: .constant(0)) {
        Text("One").tag(0)
        Text("Two").tag(1)
        Text("Three").tag(2)
    }
    .pickerStyle(.radioGroup)
}
.padding()

“

我认为这是错误的吗?是否有一种方法可以像AppKit一样掌握基线,还是我必须手动定位按钮?

I’m trying to vertically align a Text and a regular Button along their baselines in SwiftUI on macOS. I basically want to replicate this layout from the WiFi preference pane:

macOS WiFi Preference Pane - Alignment Layout

I expected that putting the said controls into an HStack with the alignment set to .firstTextBaseline would produce this layout. While this is the case for many other controls, it doesn’t seem to work with non-plain buttons. See the following example:

HStack(alignment: .firstTextBaseline) {
    Text("Text")
    
    Text("Large Text")
        .font(.title)
    
    Button {} label: {
        Text("Button")
    }
    
    Button {} label: {
        Text("Borderless")
    }
    .buttonStyle(.borderless)
    
    Button {} label: {
        Text("Plain")
    }
    .buttonStyle(.plain)
    
    Toggle("Toggle", isOn: .constant(true))
    
    Picker("Picker", selection: .constant(0)) {
        Text("One").tag(0)
        Text("Two").tag(1)
        Text("Three").tag(2)
    }
    .pickerStyle(.radioGroup)
}
.padding()

HStack Baseline Alignment

Am I holding it wrong? Is there a way to levere the baselines just like in AppKit, or do I have to position the button manually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文