有没有办法在 iOS 上将图像添加到操作表中?与苹果在应用程序商店或苹果音乐应用程序上所做的一样。我对苹果文档的基本搜索表明我没有在操作表中子类化或添加子视图。
“UIActionSheet 并非设计为子类化,也不应向其层次结构添加视图。” ——苹果文档
Swift 4:如果图像较大并且不太适合,请使用此代码和 UIImage Extension 来调整图像大小:
func showAlert() {
let alertController = UIAlertController()
let action = UIAlertAction(title: "Title", style: .default) { (action: UIAlertAction!) in
}
if let icon = icon?.imageWithSize(scaledToSize: CGSize(width: 32, height: 32)) {
action.setValue(icon, forKey: "image")
}
alertController.addAction(action)
self.present(alertController, animated: true, completion: nil)
}
并使用这个扩展:
extension UIImage {
func imageWithSize(scaledToSize newSize: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)