我想在UIAlertbox中添加Image,所以我添加以下代码。
let alertController = UIAlertController(title: "Gender", message: "" , preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "Female", style: UIAlertActionStyle.default) {
UIAlertAction in
// exit(0)
debugPrint("Press OK")
}
let cancelAction = UIAlertAction(title: "Male", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}
// Add the actions
okAction.setValue(#imageLiteral(resourceName: "female_image"), forKey: "image")
cancelAction.setValue(#imageLiteral(resourceName: "male_image"), forKey: "image")
alertController.addAction(okAction)
alertController.addAction(cancelAction)
当我运行应用程序时,只显示一个图像。这有什么问题?
请有人帮帮我吗?
答案 0 :(得分:3)
尝试使用此代码在swift3中工作
let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert)
let image = UIImage(named: "blanckstar")
let action = UIAlertAction(title: "Male", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Male")
}
action.setValue(image, forKey: "image")
let image2 = UIImage(named: "blanckstar")
let action2 = UIAlertAction(title: "Female", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Female")
}
action2.setValue(image2, forKey: "image")
alertMessage.addAction(action)
alertMessage.addAction(action2)
self.present(alertMessage, animated: true, completion: nil)
快乐的结合: - )
更改图像大小,您可以尝试使用fontAwesome只安装pod
pod'FontAwesome.swift'并导入FontAwesome_swift
这是代码.....
let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert)
let image = UIImage.fontAwesomeIcon(name: .male, textColor: UIColor.black, size: CGSize(width: 50, height: 50))
let action = UIAlertAction(title: "Male", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Male")
}
action.setValue(image, forKey: "image")
let image2 = UIImage.fontAwesomeIcon(name: .female, textColor: UIColor.black, size: CGSize(width: 50, height: 50))
let action2 = UIAlertAction(title: "Female", style: .default)
{
UIAlertAction in
// exit(0)
debugPrint("Press Female")
}
action2.setValue(image2, forKey: "image")
alertMessage.addAction(action)
alertMessage.addAction(action2)
self.present(alertMessage, animated: true, completion: nil)