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...
Asked
Active
Viewed 789 times
2
GatoCurioso
- 225
- 3
- 11
-
Please provide some code, you have already tried and edit it to the question. – loki Aug 30 '16 at 07:10
-
I thought to have the question and the answer (with code) separate is more clear... Don't you think? – GatoCurioso Aug 30 '16 at 09:01
1 Answers
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
**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):
**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
-
This is interesting but have you submitted an app to the App Store that does this? You're using undocumented API-- the `image` key. – Tom Harrington Jul 17 '17 at 20:27
-