12

How can one change the color of a UIAlertAction button, that has been added to an UIAlertController?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
borchero
  • 5,562
  • 8
  • 46
  • 72
  • 1
    This has nothing to do with Xcode. This is an API question which would be the same had you used a different IDE (or no IDE at all). – The Paramagnetic Croissant Dec 15 '14 at 13:01
  • Actually no, I wanted to know how you can change the button color of the UIKit from the CocoaTouch framework which clearly has to do with xcode – borchero Dec 15 '14 at 13:02
  • 1
    This has nothing to do with xcode. Croissant is right. And to your question, you can't change the color of the button displayed by `UIAlertController`. You should create your own alert view or action sheet if you want further customization. – wcd Dec 15 '14 at 13:05
  • @OliverBorchert Ya, it's an API question then. Cocoa Touch is an API. You can use it with Xcode, with any other IDE, or without any sort of IDE at all. Because after all, it's just a library and a set of header files which you link against and include, respectively. – The Paramagnetic Croissant Dec 15 '14 at 13:09
  • Okay then .. thanks for informing me it isn't possible – borchero Dec 15 '14 at 13:09
  • 2
    See this post: http://stackoverflow.com/questions/24154889/change-text-color-of-items-in-uiactionsheet-ios-8?lq=1 – Salman Zaidi Dec 15 '14 at 13:42
  • I have added above mentioned post url as answer. Mark your post as answered and solved for future usage. – Salman Zaidi Dec 17 '14 at 11:04
  • Answer: http://stackoverflow.com/a/37747769/4579593 – Evgeny Fedin Jul 05 '16 at 06:43

2 Answers2

19

No need to do some complicated things, you only need to change the tintColor property of the UIAlertController main view like this :

let alert = UIAlertController(title: "Title", message: "Some message", preferredStyle: .Alert)
alert.view.tintColor = UIColor.redColor()
let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
alert.addAction(action)

This way your buttons tint will be red (or whatever color you want)

Sylver
  • 2,285
  • 3
  • 29
  • 40
4

Refer following post:

Change Text Color of Items in UIActionSheet - iOS 8

Although it's related to changing color of UIActionSheet items but one user has answered according to change color of UIAlertAction items.

Community
  • 1
  • 1
Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
  • This is not the correct answer. See below for the correct answer. – Dmitry Nov 27 '19 at 23:55
  • actually the fix is as simple as `UIAlertAction.setValue(UIColor.blue, forKey: "titleTextColor")` – Radu Ursache Jun 17 '21 at 13:49
  • ** let alert = UIAlertController(title: "Delete Note", message: "Are you sure to delete this note?", preferredStyle: .alert) alert.view.tintColor = UIColor.green ** – Ali Raza Feb 02 '23 at 12:25