1

enter image description here

I can change the font color of all the actions to be green like the picture above using the code below:

func showAlertSheet() {
    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    let accountAction = UIAlertAction(title: "Account", style: .default) { (action) in
        self.performSegue(withIdentifier: "toAccountVC", sender: nil)
    }

    let actionPhotoLibrary = UIAlertAction(title: "Logout", style: .default) { (action) in

    }

    let actionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)


    actionSheet.addAction(accountAction)
    actionSheet.addAction(actionPhotoLibrary)
    actionSheet.addAction(actionCancel)

    actionSheet.view.tintColor = UIColor(red: 0/255, green: 129/255, blue: 58/255, alpha: 1)

    self.present(actionSheet, animated: true, completion: nil)
}

But I need to change Logout action to be red like the picture below enter image description here

I have read this thread UIAlertController custom font, size, color but I can't find the way to change font color on particular UIAlertController action sheet.

What do I have to do?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alexa289
  • 8,089
  • 10
  • 74
  • 178

2 Answers2

1

Use .destructive instead of .default for the action style.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

There is no direct method to set the specific button on action sheet. You can do it this way to make it happaned.

  1. Create a custom UIAlertController subclass, let's call it TestAlertController.
  2. Override viewDidAppear method to find the label that you want to modify.
  3. Just use the TestAlertController.

Here is the code in ipod(ios 9.3), you can have a try.

class TestAlertController: UIAlertController {
    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let subViews = self.view.subviews[0].subviews[1].subviews[0].subviews
    for view in subViews {
        if view.isKind(of: NSClassFromString("UICollectionView")!) {
            if let modifyView = view.subviews[1].subviews[0].subviews[0].subviews[0].subviews[0] as? UILabel {
                print(modifyView)
                modifyView.tintColor = UIColor.red
            }
        }
    }
}
}

But it's different in iPhone8 plus simulator (iOS 11), the code is:

    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if #available(iOS 9.0, *) {
        if let stackView = self.view.subviews[0].subviews[0].subviews[1].subviews[1].subviews[0].subviews[0] as? UIStackView {
            let view = stackView.arrangedSubviews[2]
            if let label = view.value(forKeyPath: "_actionContentView._label") as? UILabel {
                label.tintColor = UIColor.red
            }
            print("hard to locate...")
        }
    }
}

Here is the Screen Shot: enter image description here

This method needs to check on each iOS version or maybe iPhone. I don't think it a good way. But I can't find a better way. Could anyone tell me a better way?