2

I am trying to make UIAlertController that looks like this:

Alert screenshot

How can we customize the UIAlertController to get the result something same as this picture ?

4
  • 3
    You need to make your own pop-up with that design Commented Aug 13, 2015 at 20:35
  • @JAL all what i have found was for UIAlertView, which is deprecated, i have not idea where to start with this Commented Aug 13, 2015 at 20:39
  • @EduardoIglesias do u mean implementing everything implemented in UIAlertController from the beginning? with dim effect Commented Aug 13, 2015 at 20:40
  • 2
    You cannot customize UIAlertController. Apple specifically says DO NOT subclass UIAlertController. You will have to make your own custom controller. Commented Aug 13, 2015 at 21:41

1 Answer 1

6

What you are trying to do is a popover, for current versions of iOS you can achieve the same effect for both iPad and iPhone.

1.- Start by building your design on Storyboard or a xib. and then reference it.

2.- then present it as a popover.

3.- maybe you will want to implement popoverdelegates to avoid wrong positions when rotating the device.

for example:

  private static func presentCustomDialog(parent: UIViewController) -> Bool {
        /// Loads your custom from its xib or from Storyboard
        if let rateDialog = loadNibForRate() {
            rateDialog.modalPresentationStyle = UIModalPresentationStyle.Popover
            rateDialog.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
            let x = parent.view.center

            let sourceRectX : CGFloat

            let maximumDim = max(UIScreen.mainScreen().bounds.height, UIScreen.mainScreen().bounds.width)
            if maximumDim == 1024 { //iPad
                sourceRectX = x.x
            }else {
                sourceRectX = 0
            }

            rateDialog.popoverPresentationController?.sourceView = parent.view
            rateDialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
            rateDialog.popoverPresentationController?.sourceRect = CGRectMake(sourceRectX, x.y, 0, 0)
            rateDialog.popoverPresentationController?.popoverLayoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
            rateDialog.popoverPresentationController?.delegate = parent

            rateDialogParent = parent

            dispatch_async(dispatch_get_main_queue(), {
                parent.presentViewController(rateDialog, animated: true, completion: nil)
            })
            return true
        }
        return false
    }

Update: to achieve, point 3... on your parent UIViewController.

public class MyParentViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    /**
    This function guarantees that the CustomDialog is always centered at parent, it locates the Dialog view
     */
    public func popoverPresentationController(popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) {
        let x = popoverPresentationController.presentingViewController.view.center
        let newRect = CGRectMake(x.x, x.y, 0, 0)
        rect.initialize(newRect)
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

great idea, i am giving it a try
how to make this full screen? is it possible to show this popover to full screen?
you may achieve that (not tested) by setting it origin (anchor) to (0,0) and it width and height to those at UIScreen.mainScreen().bounds's width and height ...and removing any arrows you may have set for this popover.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.