bunuelcubosoto
bunuelcubosoto

Reputation: 99

Title in SwiftUI Menu/Contextual Menu

Is there any way to add title (Debug Menu) to the Menu in iOS? I know it's possible in Swift/UIKit but I don't find a way in SwiftUI.

enter image description here

Upvotes: 9

Views: 4367

Answers (3)

juliand665
juliand665

Reputation: 3324

Inspired by @narek.sv's answer, I tried giving the sections headers, and it turns out that works perfectly!

Menu("Example") {
    Section("Header 1") {
        Button("Item 1") {}
        Button("Item 2") {}
    }
    Section("Header 2") {
        Button("Item 1") {}
        Button("Item 2") {}
    }
}

screenshot of a menu with headers

Upvotes: 15

narek.sv
narek.sv

Reputation: 1575

There is no official solution for that, but you can achieve something similar using Sections

Menu(content: {
   Section {
      Text("Title")
   }
            
   Section {
      Button("Button 1") {
         print("action 1")
      }
                
      Button("Button 2") {
         print("action 2")
      }
   }
}, label: {
   ...
})

example

Upvotes: 11

Lukas
Lukas

Reputation: 281

Unfortunately, Apple does not provide this function. The title appears as a "button" that calls up the menu, but not again in the menu itself. Maybe this can be achieved with an own menustyle. You can find information about this here: https://developer.apple.com/documentation/swiftui/menu

Upvotes: 0

Related Questions