gpt4 book ai didi

swift - 如何在 UIAlertController 中为 UIImage 着色

转载 作者:可可西里 更新时间:2023-11-01 00:58:32 24 4
gpt4 key购买 nike

如何创建一个带有取消按钮和其他三个带有图标的按钮的 UIAlertController?我支持iOS 8以上。每个条目都应该着色...

最佳答案

我尝试并搜索了很多,终于找到了答案。在这里:

**使用图标创建 `UIAlertController`:**

重要的部分是使用 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)

这就是我得到的。好的到目前为止😀

UIAlertController with cancel button and 3 entries with icon


**为条目着色**

然后我尝试对所有内容进行着色,并在 UIAlertController 的实例化下方添加了以下行: optionMenu.view.tintColor = Colors.getColors().primaryTintColor

这在 iOS8 上看起来相当不错,但当我在 iOS9 上测试相同的代码时,我感到很惊讶(本地化差异是因为模拟器中的不同设置):

iOS8
Tinted UIAlertController with iOS8

iOS9
Tinted UIAlertController with iOS9

**解决方案**

我花了很长时间,但后来我找到了解决方案。您必须在 呈现 UIAlertController 后设置色调。 😎

这是最终代码:

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

关于swift - 如何在 UIAlertController 中为 UIImage 着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39220721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com