我正在尝试创建一个UIAlertAction,该按钮的按钮具有黑色文本,我也想分配图像:
let alert = UIAlertController(title: nil,
message: nil,
preferredStyle: .actionSheet)
let image = UIImage(named: "actionSheetGallery")
let action = UIAlertAction(title: "Choose Picture From Gallery", style: .default, handler: selectFromGallery)
action.setValue(image, forKey: "image")
let image1 = UIImage(named: "actionSheetCamera")
let action1 = UIAlertAction(title: "Take Picture from Camera", style: .default, handler: takePhoto)
action1.setValue(image1, forKey: "image")
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel, handler: nil)
alert.addAction(action1)
alert.addAction(action)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
我可以分配图像或文本颜色,我们如何分配两者。 并且您看到的图像背景也会发生变化。
答案 0 :(得分:2)
我认为action1.setValue(image1, forKey: "image")
可能很危险:当您使用私有api时,Apple可能拒绝您的应用程序。 可以,有时我使用私有api,什么也没发生。
无论如何,如果要更改文本颜色,可以为警报控制器的视图设置淡色:
alert.view.tintColor = .orange // or whatever
如果您确实要放置图像,则图像本身将继承视图的色调颜色(默认为蓝色)。为了避免使用色彩,可以更改图像的渲染模式:
let image = UIImage(named: "actionSheetGallery")?.withRenderingMode(.alwaysOriginal)
答案 1 :(得分:1)
此行是非法的:
action1.setValue(image1, forKey: "image")
UIAlertAction无法包含图像。如果要自定义操作表的外观,请创建自己的呈现视图控制器(毕竟,这实际上是警报控制器的全部)。
答案 2 :(得分:0)
尝试以下功能
func myAlert(){
let refreshAlert = UIAlertController(title: "Unlike Article", message: "Are you sure you want to unlike this Article?", preferredStyle: UIAlertController.Style.alert)
let cancel = UIAlertAction(title: "CANCEL", style: .default, handler: { (action: UIAlertAction!) in
return
})
let image = resizeImage(image: #imageLiteral(resourceName: "twitter"), targetSize: CGSize(width: 25, height: 25))
let unLike = UIAlertAction(title: "UNLIKE", style: .destructive, handler: { (action: UIAlertAction!) in
return
})
unLike.setValue(image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), forKey: "image")
refreshAlert.addAction(cancel)
refreshAlert.addAction(unLike)
refreshAlert.view.tintColor = UIColor.black
self.present(refreshAlert, animated: true, completion: nil)
}
答案 3 :(得分:-1)
您可以通过以下方式实现上述目标:
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let actionCamera = UIAlertAction(title: "Camera", style: .default, handler: { (_) -> Void in
//code for camera
})
actionCamera.setValue(UIImage(named: "imageName")?.withRenderingMode(.alwaysOriginal), forKey: "image")
actionCamera.setValue(CATextLayerAlignmentMode.left, forKey: "titleTextAlignment")
actionSheet.addAction(actionCamera)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.view.tintColor = UIColor.black
self.present(actionSheet, animated: true, completion: nil)
希望它会有所帮助;)