Sauvik Dolui Sauvik Dolui - 1 month ago 30
Swift Question

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

I need to add some extra

UI
component in the body of
UIAlertController
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

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.

Demonstration

Comments