简体   繁体   中英

Changing the toolbar item placement on iPad

Let's take the Shortcuts app for example.

On the iOS App: iOS 上的快捷方式

On the iPadOS App: 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?

you can check for the used device type:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM