noblerare noblerare -4 years ago 130
Swift Question

Swift - How do I place a page-view within a UIAlertController?

I am using Swift 3, Xcode 8.2.

I have an application where the user launches the iPhone camera and I give them a popup with instructions on how to take a good picture. I want there to be a way to create pages within the

. I did a quick sketch of what I want to achieve.

enter image description here

Code wise, I am not sure what to do:

func displayInstructions() {

let insController = UIAlertController(title: "Instructions", message: "Step 1: Do this.", preferredStyle: .alert)

let actionDone = UIAlertAction(title: "OK", style: .cancel) { (action:UIAlertAction) in
//This is called when the user presses the cancel button.
print("You've pressed the done button");

//Add the buttons

// Some way to addSubviews here??
let pageViewController: UIPageViewController

//Present the instruction controller
self.present(insController, animated: true, completion: nil)

Any help would be greatly appreciated. Thank you!

Answer Source

There is a property on UIAlertController, not advertised in public API but that seem usable without trouble going through the app store review. So, using KVC, you can set the contentViewController of the alert controller.

let pageViewController: UIPageViewController

// configure pageViewController...
insController.setValue(pageViewController, forKey: "contentViewController")

You can also set the size of the contentViewController by setting preferredContentSize.height on it

pageViewController.preferredContentSize.height = 180

This is what the result looks like with an empty page view controller

UIAlertController with nested page view controller

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download