2

How can I create an UIAlertController with a cancel button and three other buttons with an icon? I am supporting iOS 8 upwards. Each entry should be tinted...

GatoCurioso
  • 225
  • 3
  • 11

1 Answers1

3

I tried and searched a lot and finally found the answer. Here it is:

**Create the `UIAlertController` with icons:**

The important part is the addition of the icons with action1.setValue(UIImage(named: "Icon1"), forKey: "image")

let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

let action1 = UIAlertAction(title: "Test 1", style: .Default, handler: {
    [unowned self] (alert: UIAlertAction!) -> Void in
    print("Action 1")
})
action1.setValue(UIImage(named: "Icon1"), forKey: "image")

let action2 = UIAlertAction(title: "Test 2", style: .Default, handler: {
    [unowned self] (alert: UIAlertAction!) -> Void in
    print("Action 2")
})
action2.setValue(UIImage(named: "Icon2"), forKey: "image")

let action3 = UIAlertAction(title: "Test 3", style: .Default, handler: {
    [unowned self] (alert: UIAlertAction!) -> Void in
    print("Action 3")
})
action3.setValue(UIImage(named: "Icon3"), forKey: "image")

let cancelAction = UIAlertAction(title: "Close", style: .Cancel, handler: nil)

optionMenu.addAction(action1)
optionMenu.addAction(action2)
optionMenu.addAction(action3)
optionMenu.addAction(cancelAction)

presentViewController(optionMenu, animated: true, completion: nil)

This is what I got. Ok so far

UIAlertController with cancel button and 3 entries with icon


**Tinting the entries**

Then I tried to tint everything and I added the following line underneath the instantiation of UIAlertController: optionMenu.view.tintColor = Colors.getColors().primaryTintColor

This looked pretty good with iOS8, but as I tested the same code with iOS9 I was surprised (the localization difference is because of different settings in the simulators):

iOS8
Tinted UIAlertController with iOS8

iOS9
Tinted UIAlertController with iOS9

**The solution**

I took me quite a while but then I found the solution. You have to set the tint color after you present the UIAlertController.

This is the final code:

let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

let action1 = UIAlertAction(title: "Test 1", style: .Default, handler: {
    [unowned self] (alert: UIAlertAction!) -> Void in
    print("Action 1")
})
action1.setValue(UIImage(named: "Icon1"), forKey: "image")

let action2 = UIAlertAction(title: "Test 2", style: .Default, handler: {
    [unowned self] (alert: UIAlertAction!) -> Void in
    print("Action 2")
})
action2.setValue(UIImage(named: "Icon2"), forKey: "image")

let action3 = UIAlertAction(title: "Test 3", style: .Default, handler: {
    [unowned self] (alert: UIAlertAction!) -> Void in
    print("Action 3")
})
action3.setValue(UIImage(named: "Icon3"), forKey: "image")

let cancelAction = UIAlertAction(title: "Close", style: .Cancel, handler: nil)

optionMenu.addAction(action1)
optionMenu.addAction(action2)
optionMenu.addAction(action3)
optionMenu.addAction(cancelAction)

presentViewController(optionMenu, animated: true, completion: nil)
optionMenu.view.tintColor = Colors.getColors().primaryTintColor
GatoCurioso
  • 225
  • 3
  • 11