iosuitableviewuimenuuiaction

How to programmatically present a menu when single-tap a table view row, like in the iOS 16 Calendar app?


In the iOS 16 Calendar app, there is a new drop-down menu style for options like "repeat", when tapping any place of the row, a menu appeared. And there is a chevron up and chevron down icon at the right side of the table view cell.

How to do this in iOS 16? The context menu is triggered by long press, but this new style is by single-tap.

iOS 16 Calendar app create new event page with repeat option


Solution

  • In SwiftUI:

    import SwiftUI
    
    struct SettingsView: View {
        @State private var selectedFlavor: Flavor = .chocolate
    
        var body: some View {
            List {
                Picker("Flavor", selection: $selectedFlavor) {
                    Text("Chocolate").tag(Flavor.chocolate)
                    Text("Vanilla").tag(Flavor.vanilla)
                    Text("Strawberry").tag(Flavor.strawberry)
                }
            }
        }
    }
    
    struct SettingsView_Previews: PreviewProvider {
        static var previews: some View {
            SettingsView()
        }
    }
    
    enum Flavor: String, CaseIterable, Identifiable {
        case chocolate, vanilla, strawberry
        var id: Self { self }
    }
    

    In UIKit, there is a collection view API to do it by providing a UIMenu: cell.accessories = [.popUpMenu(menu)] https://developer.apple.com/documentation/uikit/uicellaccessory-swift.struct/popupmenu(_:displayed:options:selectedelementdidchangehandler:)