Search code examples
iosswiftuialertcontrolleruicolor

How to give background colour for alert OK action button and how to give top space for title in alert in swift


I have created alert with image view i need that image to rounded and i need to give background colour to alert done button. please help me in the code.

here is my alert code:

 func showAlert(){

    let alertController = UIAlertController(title:"      Profile created    \n     Sccessfully     ", message: "", preferredStyle: .alert)
    let doneAlert = UIAlertAction(title: "DONE", style: .default, handler: { (action) -> Void in

        self.navigationController?.popViewController(animated: true)
    })
    alertController.addAction(doneAlert)

    image.layer.cornerRadius = image.frame.height/2
    image.layer.borderWidth = 1
    alertController.view.addSubview(image)

    image.translatesAutoresizingMaskIntoConstraints = false
    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: alertController.view, attribute: .centerX, multiplier: 1, constant: 0))

    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .top, relatedBy: .equal, toItem: alertController.view, attribute: .top, multiplier: 1, constant: -20))

    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40))
    alertController.view.addConstraint(NSLayoutConstraint(item: image, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 40))
    self.present(alertController, animated: true, completion: nil)

}

I need alert like this

Alert


Solution

  • From the documentation:

    Important

    The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

    You're probably better off using an existing library that implements this, such as e.g. https://github.com/vikmeup/SCLAlertView-Swift, or implementing your own alert from scratch.