1

I am trying to achieve the view with a few green actions, and get the selected action to be bold in UIAlertController. And one of the actions, which is dismiss button has to be divided from others and be colored with red.

I am trying to add them with the styles .cancel allows to show dismiss button, but it is bold, but green. How do I achieve this?

expected view:

expected view

my current code:

let alertController
        = UIAlertController(title: nil,
                            message: nil,
                            preferredStyle: .actionSheet)

    let sortByDateAction = UIAlertAction(title: "По дате",
                                         style: .default,
                                         handler: {(action: UIAlertAction!) in
                                            if self.sortBy != "date" {
                                                self.page = 1
                                                self.sortBy = "date"
                                                self.loadTenders()
                                            }
    })
    let moreExpensiveSortAction = UIAlertAction(title: "Дороже",
                                                style: .destructive,
                                                handler: {(action: UIAlertAction!) in
                                                    if self.sortBy != "priceHigh" {
                                                        self.page = 1
                                                        self.sortBy = "priceHigh"
                                                        self.loadTenders()
                                                    }
    })
    let cheaperSortAction = UIAlertAction(title: "Дешевле",
                                          style: .default,
                                          handler: {(action: UIAlertAction!) in
                                            if self.sortBy != "priceLow" {
                                                self.page = 1
                                                self.sortBy = "priceLow"
                                                self.loadTenders()
                                            }
    })

    alertController
        .addAction(sortByDateAction)
    alertController
        .addAction(moreExpensiveSortAction)
    alertController
        .addAction(cheaperSortAction)

    alertController.preferredAction = sortByDateAction

    alertController
        .addAction(UIAlertAction(title: "Dismiss",
                                 style: .cancel,
                                 handler: nil))
Miras Maratuly
  • 216
  • 2
  • 14

2 Answers2

2
let cancelAlert = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:nil)
        cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
        alertController.addAction(cancelAlert)

try this code.

gEeKyMiNd
  • 245
  • 1
  • 9
  • Thanks, that did one of my problems, but not the one I am asking a solution for. I am questioning on how to make one of the actions title font weight bold? – Miras Maratuly Jun 07 '17 at 10:07
  • yes, I have tried that, but it only works for UIAlertControllerStyle.alert, and I am using an .actionSheet. – Miras Maratuly Jun 08 '17 at 05:04
1

The color of the text in the action sheet is nothing but the tint color of the view. Set this as per your color requirement.

Furthermore, To change the font color of cancel button: Set the value for the titleTextColor property of the UIAlertAction

Here is a sample code:

func showCaptureOptions() {

                let actionSheet = UIAlertController(title: "Capture Type", message: "", preferredStyle: .actionSheet)

                let cameraAction = UIAlertAction(title: "Camera", style: .default) { [weak self](action) in
                    self?.initializeImagePicker(captureType: .camera)
                }

                let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default) { [weak self](action) in
                    self?.initializeImagePicker(captureType: .photoLibrary)
                }

                let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
                }

                cancelAction.setValue(UIColor.red, forKey: "titleTextColor"). /// The cancel button will be red

                actionSheet.view.tintColor = UIColor.green /// The action sheet options would be green

                actionSheet.addAction(cameraAction)
                actionSheet.addAction(photoLibraryAction)
                actionSheet.addAction(cancelAction)

                DispatchQueue.main.async {
                    self.present(actionSheet, animated: true, completion: nil)
                }
            }
Zarif Ahmed
  • 343
  • 2
  • 9
  • Thanks, that did one of my problems, but not the one I am asking a solution for. I am questioning on how to make one of the actions title font weight bold? – Miras Maratuly Jun 07 '17 at 10:06
  • The font attribute for UIAlertController is a private property. it is advised not to change this as you will be going against the guidelines if you change it. With that being said, it can still be done. Check this: https://stackoverflow.com/questions/25988639/is-it-possible-to-edit-uialertaction-title-font-size-and-style/27518769#27518769 – Zarif Ahmed Jun 07 '17 at 10:59
  • Thanks, but it does change the font for every action, but I need it only for one action. – Miras Maratuly Jun 07 '17 at 11:54