Reputation: 13803
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
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?
Upvotes: 1
Views: 557
Reputation: 79
There is no direct method to set the specific button on action sheet. You can do it this way to make it happaned.
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...")
}
}
}
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?
Upvotes: 0