Skip to content

Aeastr/MenuWithAView

MenuWithAView Icon

MenuWithAView

Add accessory views to context menu interactions using UIKit's private _UIContextMenuAccessoryView.

Swift 6.0+ iOS 16+ License: MIT Experimental

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.

MenuWithAView demo

Overview

  • Attach custom accessory views to any .contextMenu
  • Control placement, location, alignment, and tracking axis
  • Programmatic dismissal via ContextMenuProxy
  • DocC documentation included

Installation

dependencies: [
    .package(url: "https://github.com/Aeastr/MenuWithAView.git", from: "1.0.0")
]
import MenuWithAView

Or in Xcode: File > Add Packages… and enter https://github.com/Aeastr/MenuWithAView

Usage

Basic Usage

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)
    }

With Programmatic Dismissal

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))
    }

Customization

Parameters

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

How It Works

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.

Contributing

Contributions welcome. See the Contributing Guide for details.

License

MIT. See LICENSE for details.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages