0

can anyone please tell me a way of adding in an image into a UIAlertController? This is the code I have generated to produce the Alert Controller.

I believe I need to add a subview but not sure?

let myAlert = UIAlertController(title: "Congratualtions", message: "Correct answer selected, move on to next level", preferredStyle: UIAlertControllerStyle.alert)

    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default)
    {
        action in self.dismiss(animated: true, completion: nil)
    }

    myAlert.addAction(okAction);
    self.present(myAlert, animated: true, completion:nil)

The code works but i do not know how add an image into the action sheet...

  • use UIAlertControllerStyle.actionSheet for action sheet, image problem is already answered here http://stackoverflow.com/questions/28340836/swift-add-image-to-alert-view – kuzdu Mar 16 '17 at 12:10
  • I have followed the link provided but have an error message that says 'CGRectMake' is unavailable for swift? – Keiron Morris Mar 16 '17 at 12:29
  • oh it's swift 2: google CGRectMake to Swift 3. It should be something like that: image.frame = CGRect(x: 0,y: 0, width: 150, height: 150) – kuzdu Mar 16 '17 at 12:40

1 Answers1

0

You can use this library (MZFormSheetPresentationController) to create the AlertViews with Images,Buttons and lots of functions. You can design viewController as you want...

func formSheetControllerWithNavigationController() -> UINavigationController {
                return self.storyboard!.instantiateViewController(withIdentifier: "formSheetController") as! UINavigationController
}

func passDataToViewControllerAction() {

            let navigationController = self.formSheetControllerWithNavigationController()
            let formSheetController = MZFormSheetPresentationViewController(contentViewController: navigationController)
            formSheetController.presentationController?.isTransparentTouchEnabled = false
            formSheetController.presentationController?.shouldCenterVertically = true
            formSheetController.presentationController?.shouldCenterHorizontally = true
            formSheetController.presentationController?.shouldDismissOnBackgroundViewTap = true

            let yourViewController = navigationController.viewControllers.first as! EventDisplayViewController

            yourViewController.delegate = self



            self.present(formSheetController, animated: true, completion: nil)
}
GayashanK
  • 1,195
  • 2
  • 13
  • 27