Swift - UIAlert with image

流过昼夜 提交于 2019-12-13 22:27:10

问题


I have this code:

var zdjecieGlowne: UIImage? = nil
let alert = UIAlertController(title:"MyTitle", message: "My Message Box ", preferredStyle: UIAlertControllerStyle.alert)
let saveAction = UIAlertAction(title: "MyTitle", style: .default, handler: nil)
//saveAction.setValue(UIImage(named: "logo")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")
saveAction.setValue(zdjecieGlowne, forKey: "image")
dump(zdjecieGlowne)
alert.addAction(saveAction)
alert.addAction(UIAlertAction(title: "Option 1", style: UIAlertActionStyle.default, handler: { alertAction in
    //alert.dismiss(animated: true, completion: nil)
    print("1 pressed")
}))

alert.addAction(UIAlertAction(title: "Option 2", style: UIAlertActionStyle.default, handler: { alertAction in
    //alert.dismiss(animated: true, completion: nil)
    print("2 pressed")
}))
alert.addAction(UIAlertAction(title: "Option 3", style: UIAlertActionStyle.default, handler: { alertAction in
    //alert.dismiss(animated: true, completion: nil)
    print("3 pressed")
}))

self.present(alert, animated: true, completion: nil)

As a result, I receive Alert with a blue background instead of a picture. Why is it like that?


回答1:


Try the below code in swift

saveAction.setValue(UIImage(named: "name.png").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")

or try this

saveAction.setValue(zdjecieGlowne.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")



回答2:


Please try

[saveAction setValue:[[UIImage imageNamed:@"image-name.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

In Swift:

saveAction.setValue(UIImage(named: "image-name.jpg").withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")



回答3:


You can add your image like the below code

var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40))
imageView.image = yourImage
alert.view.addSubview(imageView)


来源:https://stackoverflow.com/questions/49770018/swift-uialert-with-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!