更改iPad上的工具栏项目位置

发布于 2025-01-22 22:39:46 字数 1303 浏览 2 评论 0原文

让我们以快捷方式应用程序。

在iOS应用程序上:

在iPados应用程序上:

如您所见,工具栏项目在上。

我的代码看起来像这样:

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("This is a test app.")
                .navigationTitle("All Shortcuts")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button("Select") {
                            // Do something...
                        }
                        
                        Button(action: {
                            // Do something else...
                        }) {
                            Label("Some Other Action", systemImage: "plus")
                        }
                    }
                }
        }
    }
}

这是在iPhone上的预期工作,但是在iPad上,工具栏项目出现在工具栏的后端。我希望工具栏项目在iPhone的后端和iPad的领先端,就像快捷应用程序一样。我该如何实现这种行为?

Let's take the Shortcuts app for example.

On the iOS App:
Shortcuts on iOS

On the iPadOS App:
Shortcuts on iPadOS

As you can see, the toolbar items were on .navigationBarTrailing on iPhone, but it moved to .navigationBarLeading on iPad.

My code looks like this:

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("This is a test app.")
                .navigationTitle("All Shortcuts")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button("Select") {
                            // Do something...
                        }
                        
                        Button(action: {
                            // Do something else...
                        }) {
                            Label("Some Other Action", systemImage: "plus")
                        }
                    }
                }
        }
    }
}

This works as expected on iPhone, but on iPad the toolbar items appear at the trailing end of the toolbar. I want the toolbar items to be on the trailing end for iPhones and on the leading end for iPads, just like the Shortcuts app. How can I achieve this behavior?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味超标? 2025-01-29 22:39:46

您可以检查使用的设备类型:

ToolbarItemGroup(placement: UIDevice.current.userInterfaceIdiom == .pad ?
                           .navigationBarLeading : .navigationBarTrailing) {

you can check for the used device type:

ToolbarItemGroup(placement: UIDevice.current.userInterfaceIdiom == .pad ?
                           .navigationBarLeading : .navigationBarTrailing) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文