Warning
This package uses a private API (_UIContextMenuAccessoryView) which may be unstable and could change or break in future iOS releases. Use with caution. App Store submissions are at your own risk.
- Attach custom accessory views to any
.contextMenu - Control placement, location, alignment, and tracking axis
- Programmatic dismissal via
ContextMenuProxy - DocC documentation included
dependencies: [
.package(url: "https://github.com/Aeastr/MenuWithAView.git", from: "1.0.0")
]import MenuWithAViewOr in Xcode: File > Add Packages… and enter https://github.com/Aeastr/MenuWithAView
Text("Turtle Rock")
.padding()
.contextMenu {
Button(action: {}) {
Label("Button", systemImage: "circle")
}
}
.contextMenuAccessory(
placement: .center,
location: .preview,
alignment: .leading,
trackingAxis: .yAxis
) {
Text("Accessory View")
.font(.title2)
.padding(8)
.background(Color.blue.opacity(0.6))
.clipShape(RoundedRectangle(cornerRadius: 12))
.padding(16)
}Text("Turtle Rock")
.padding()
.contextMenu {
Button(action: {}) {
Label("Button", systemImage: "circle")
}
}
.contextMenuAccessory(placement: .center) { proxy in
VStack {
Text("Accessory View")
.font(.title2)
Button("Dismiss") {
proxy.dismiss()
}
.buttonStyle(.borderedProminent)
}
.padding()
.background(Color.blue.opacity(0.6))
.clipShape(RoundedRectangle(cornerRadius: 12))
}| Parameter | Description | Default |
|---|---|---|
placement |
Where the accessory attaches relative to the context menu | .center |
location |
Where the accessory appears | .preview |
alignment |
How the accessory aligns within its container | .leading |
trackingAxis |
Axis along which the accessory tracks user interaction | [.xAxis, .yAxis] |
The accessory view builder has two variants:
- Simple:
@ViewBuilder accessory: () -> AccessoryView - With proxy:
@ViewBuilder accessory: (ContextMenuProxy) -> AccessoryView
MenuWithAView wraps UIKit's private _UIContextMenuAccessoryView API, discovered and documented by @sebjvidal.
Since this relies on private APIs, behavior may change between iOS versions. The package is provided for experimentation and learning purposes.
Contributions welcome. See the Contributing Guide for details.
MIT. See LICENSE for details.
