admin管理员组

文章数量:1200781

I have a View in a NavigationSplitView with a .toolbar . When placed in a horizontally compact configuration (e.g phone, slide-over or split) the native behaviour is to collapse extra buttons into a context menu.

When touched, the context menu will not display the collapsed items.

How do I get the context menu to display the extra toolbar items.

import SwiftUI

struct ContentView: View {
    
    @State var selection: Int? = nil
    
    //sugar
    func label() -> Image {
        Image(systemName: "eye")
    }
    
    var body: some View {
        NavigationSplitView {
            List(selection: $selection) {
                ForEach(0..<10) { item in
                    Text("hey \(item)").id(item)
                }
            }
        } detail: {
            VStack {
                Text("Hello, world!")
            }
            .padding()
            .navigationTitle("Woo")
            .toolbar {
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
            }
        }

    }
}

I have a View in a NavigationSplitView with a .toolbar . When placed in a horizontally compact configuration (e.g phone, slide-over or split) the native behaviour is to collapse extra buttons into a context menu.

When touched, the context menu will not display the collapsed items.

How do I get the context menu to display the extra toolbar items.

import SwiftUI

struct ContentView: View {
    
    @State var selection: Int? = nil
    
    //sugar
    func label() -> Image {
        Image(systemName: "eye")
    }
    
    var body: some View {
        NavigationSplitView {
            List(selection: $selection) {
                ForEach(0..<10) { item in
                    Text("hey \(item)").id(item)
                }
            }
        } detail: {
            VStack {
                Text("Hello, world!")
            }
            .padding()
            .navigationTitle("Woo")
            .toolbar {
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
                ToolbarItem{ Button(action: { }, label: { label() }) }
            }
        }

    }
}
Share Improve this question edited Jan 22 at 12:49 jonrsharpe 122k30 gold badges266 silver badges473 bronze badges asked Jan 22 at 12:49 Warren BurtonWarren Burton 17.4k3 gold badges54 silver badges76 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Although it's legal and valid SwiftUI to place a Button with a Image label into a toolbar:

ToolbarItem{ Button(action: { }, label: { Image(systemName: "eye") }) }

To work with all trait configurations the label needs to be a Label

ToolbarItem{ Button(action: { }, label: { Label("yo", systemImage: "eye") }) }

yielding the desired context menu.

struct ContentView: View {
    
    @State var selection: Int? = nil
    
    //sugar
    func label(_ msg: Int) -> Label<Text, Image> {
        Label("yo \(msg)", systemImage: "eye")
    }
    
    var body: some View {
        NavigationSplitView {
            List(selection: $selection) {
                ForEach(0..<10) { item in
                    Text("hey \(item)").id(item)
                }
            }
        } detail: {
            VStack {
                Text("Hello, world!")
            }
            .padding()
            .navigationTitle("Woo")
            .toolbar {
                ToolbarItem{ Button(action: { }, label: { label(1) }) }
                ToolbarItem{ Button(action: { }, label: { label(2) }) }
                ToolbarItem{ Button(action: { }, label: { label(3) }) }
                ToolbarItem{ Button(action: { }, label: { label(4) }) }
                ToolbarItem{ Button(action: { }, label: { label(5) }) }
                ToolbarItem{ Button(action: { }, label: { label(6) }) }
            }
        }

    }
}

本文标签: swiftuiToolbar in Horizontal Compact mode doesn39t display overflowed itemsStack Overflow