January 31, 2018 · Ralf Ebert » iOS Developer Blog »

Choice popover

While designing and coding an app, I often want to add a few buttons temporarily to play around with different options and variants. This snippet is a little bit of example and helper code to quickly provide a button to select an option in a pop-over - for example an option direction that is represented as an enum value:

Selecting a choice from a popover

Showing a UIViewController as popover

class ExampleViewController : UIViewController {

    @IBAction func showDirectionPopup(_ sender: UIView) {
        let controller = /* create UIViewController here */
        controller.modalPresentationStyle = .popover
        controller.preferredContentSize = CGSize(width: 300, height: 200)
        let presentationController = controller.presentationController as! UIPopoverPresentationController
        presentationController.sourceView = sender
        presentationController.sourceRect = sender.bounds
        presentationController.permittedArrowDirections = [.down, .up]
        self.present(controller, animated: true)
    }

}

Showing a popover on iPhone in portrait mode

By default, the iPhone in portrait mode doesn’t show the popover but does a modal transition. There is a workaround by using the UIPopoverPresentationControllerDelegate, which can be enabled by this handy AlwaysPresentAsPopover class.

It can be used like this:

class ExampleViewController : UIViewController {

    // ...
    
    @IBAction func showDirectionPopup(_ sender: UIView) {
        let controller = /* create UIViewController here */
        controller.preferredContentSize = CGSize(width: 300, height: 200)
        showPopup(controller, sourceView: sender)
    }
    
    private func showPopup(_ controller: UIViewController, sourceView: UIView) {
        let presentationController = AlwaysPresentAsPopover.configurePresentation(forController: controller)
        presentationController.sourceView = sourceView
        presentationController.sourceRect = sourceView.bounds
        presentationController.permittedArrowDirections = [.down, .up]
        self.present(controller, animated: true)
    }

}

Quickly creating a table view controller to select an item

To quickly create a table view controller to select an item from I use the little generic helper class ArrayChoiceTableViewController.

let items = ["Bob", "Alice", "Carol"]
let controller = ArrayChoiceTableViewController(items) { (name) in
    print("\(name) selected")
}

It also works for arbitrary objects - by default it uses String(describing:), but you can specify a block that provides the labels for the items:

let items = [URL(string: "http://www.example.com")!, URL(string: "http://www.test.com")!]
let controller = ArrayChoiceTableViewController(items, labels: { $0.host ?? "-" }) { (url) in
    print("\(url) selected")
}
Btn download 3c20f11b8e Download the finished example project
Btn read 3c0e607615 iOS Developer Blog
Btn subscribe 930758687e Subscribe: Email · Twitter
Btn training bbbdf557d2 Next iOS training: 25. Februar - 01. März 2019, Stuttgart
Btn about 5378472193 About me · Contact