Sauvik Dolui Sauvik Dolui - 1 year ago 151
Swift Question

How to add my custom UI component in the body of UIAlertController for iOS 9-10?

I need to add some extra

component in the body of
to create an alert like the following design.

AlertController with Custom Body

I did not find any public API from Apples's Documentation. Can anyone help me to how to add the extra lable and checkbox in that alert body?

Answer Source

After spending an hour in the debug view, I was finally able to extract the body of an UIAlertController by the following code

let watingListAlert = UIAlertController(title: "Lista De espera", 
                                        message: "Algunos días seleccionados, no poseen disponibilidad en el chárter. Puede inscribirse a la lista de espera y el sistema lo tendrá en cuenta cuando algún espacio se libere\n\n\n", 
                                        preferredStyle: .alert)

if let alertContentView = watingListAlert.view.subviews[0].subviews.last?.subviews.first?.subviews.first?.subviews[0] {

    // alertContentView is the view where title and message labels are drawn
    // Create your own UI Native components and add them on this view 

I was finally able to add the extra label and check box on the body of the UIAlertController. You can see my full project at GitHub. I have created a gif of the final result.


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