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

3

6

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.

Foible answered 1/2, 2018 at 4:34 Comment(0)
D
19

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

Dormeuse answered 1/2, 2018 at 5:13 Comment(0)
V
1

You need to add them to the alertView in the right order! If you want the order to change just add the confirm button second!

alert.addAction(cancelAction)
alert.addAction(confirmAction)

Instead of:

alert.addAction(confirmAction)
alert.addAction(cancelAction)
Varityper answered 16/7, 2021 at 14:34 Comment(0)
T
0

Sure, you can. Try this

let alertView = UIAlertController(
    title: "Home",
    message: "Are you sure for delete Reminder",
    preferredStyle: .alert)
alertView.view.tintColor = .green
let confirmAction = UIAlertAction(title: "Confirm", style: .cancel) { (_) in
     // Do confirm
}
alertView.addAction(confirmAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (_) in
    // Do cancel
}
alertView.addAction(cancelAction)
self.present(alertView, animated: true, completion: nil)
Television answered 1/2, 2018 at 5:4 Comment(3)
I tried that but still showing confirm button in first place.Foible
sorry, change style to .default in confirmAction and .cancel in cancelActionTelevision
than it will show cancel button as bold but I need confirm button with bold textFoible

© 2022 - 2024 — McMap. All rights reserved.