macOS 中 ToolbarItem 中的 TextField 仍然很小
我正在尝试将 TextField 放入 macOS 应用程序的工具栏中。但是,我通过 .frame() 应用的约束似乎没有任何效果。 这是一些测试代码:
ToolbarTestApp.swift
import SwiftUI
@main
struct toolbartestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.windowStyle(.titleBar)
.windowToolbarStyle(.unified(showsTitle: false))
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
@State var text = ""
var body: some View {
Text("Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!")
.padding()
.toolbar(content: {
ToolbarItem(placement: .principal) {
TextField("example", text: $text)
.textFieldStyle(.roundedBorder)
.frame(maxWidth: .infinity)
}
})
}
}
这是结果
谁能告诉我如何使 TextField 占据工具栏的整个宽度?如果我必须使用 NSViewRepresentable,你能给我指出正确的方向吗?
谢谢
I am trying to put a TextField in the toolbar of my macOs app. However, the constraints I apply via .frame() do not seem to have any effect.
Here is some the test code:
ToolbarTestApp.swift
import SwiftUI
@main
struct toolbartestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.windowStyle(.titleBar)
.windowToolbarStyle(.unified(showsTitle: false))
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
@State var text = ""
var body: some View {
Text("Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!")
.padding()
.toolbar(content: {
ToolbarItem(placement: .principal) {
TextField("example", text: $text)
.textFieldStyle(.roundedBorder)
.frame(maxWidth: .infinity)
}
})
}
}
And here is the result
Can anyone tell me how to make the TextField take up the whole width of the toolbar? And if I have to use a NSViewRepresentable, could you please point me in the right direction?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不是第一个提出这个问题的人。显然,这篇文章也讨论了这个主题。我重现您的问题并找到解决方案的方式是使用此内容视图。
这将为您提供如下屏幕:
正如您所看到的,文本字段扩展到整个窗口。
You're not the first to come up with this problem. Apparently, this post also discussed the topic. The way I reproduced your issue and came to a solution is using this content view.
This will give you the screen like this:
As you can see, the text field expands across the entire window.