NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: Ways to let the user pick from a set number of choices (Enum)

Forums > iOS

I have this enum, which defines the type of annotations in my app. It is also used for the sections of my diffable data source. When adding a new annotation the user has to pick a type for that annotation. I have this viewcontroller (form) where the user inputs all data. My first thought would be a dropdown list for the types, but there's no such thing in swift. I have some ideas how to tackle this, but I would like to hear ideas from those who might have done this before ...


I think that can be done with either a UIPickerView or a UISegmentedControl.


Thank you, Segmented control doesn't really suit my needs, but i'll look into the picker view. UIMenu isn't available on iOS13, right ?

I'll have a look at UIMenu as well


I solved it using a context menu:

In my view controller:

        let interaction = UIContextMenuInteraction(delegate: self)

And the delegate as an extension:

//MARK: - UIContextMenuInteractionDelegate
extension AnnotationDetailsViewController: UIContextMenuInteractionDelegate {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        return UIContextMenuConfiguration(identifier: "annotationTypeMenu" as NSCopying, previewProvider: nil) { _ in
            let children: [UIMenuElement] = self.makeAnnotationTypeActions()
            return UIMenu(title: "", children: children)

    func makeAnnotationTypeActions() -> [UIAction] {
        var actions = [UIAction]()
        for type in AnnotationType.allCases {
            actions.append( UIAction(title: type.rawValue, image: type.image, identifier: nil, attributes: []) { _ in
                let annotationType = AnnotationType(rawValue: type.rawValue) ??
                self.annotation.type = annotationType
                self.configureAnnotationTypeButton(with: annotationType)
        return actions

Works nicely, but I do get an autolayout error in the console on opening the menu ...


If anyone can help with my autolayout warning:

I see more people are having these layout warnings, see the remark at the end of this post


I got an answer on the Apple developer forums saying that the layout warning is a known issue and will be solved in iOS14!


Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.