Search code examples
iosuialertcontrolleruialertaction

How to change UIAlertAction font or show UIAlertActionStyle.cancel style ActionButton on second position?


I don't want to change UILabel.appearance as it will apply to all labels.

How I can show UIAlertController that will look like below image?

Need to show bold button in the second position.

Need to show Bold Button in the Second Position

By default, when I am setting UIAlertActionStyle.cancel it shows it on the Confirm button in the first position.

It looks like this now:

It looks like this now

I did some research but I didn't find any solution.


Solution

  • Swift 4.2/Swift 5

    You need to set preferred action method,

    //Create alertController
    let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
    
    //Create and add the Confirm action
    let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: { (action) -> Void in
        //Do Something here...
    })
    alert.addAction(confirmAction)
    
    //Create and add the Cancel action
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
    
        //Do Something here...
    })
    alert.addAction(cancelAction)
    
    // Set Preferred Action method
    alert.preferredAction = confirmAction
    
    self.present(alert, animated: true, completion: nil)
    

    The output will be,

    enter image description here