有没有办法在 iOS 上将图像添加到操作表?与苹果在应用商店或苹果音乐应用上所做的一样。我对苹果文档的基本搜索表明我没有在操作表中子类化或添加子视图。
“UIActionSheet 不是为子类化而设计的,您也不应该向其层次结构添加视图。” -- 苹果文档
Swift 4:如果图像较大且不适合,请使用此代码和 UIImage 扩展来调整图像大小:
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)
}
Run Code Online (Sandbox Code Playgroud)
并使用此扩展名:
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
}
}
Run Code Online (Sandbox Code Playgroud)
要在操作表中添加图像,您需要执行以下操作...
迅速
let image = UIImage(named: "myImageName")
let action = UIAlertAction(title: "Action 1", style: .default, handler: nil);
action.setValue(image?.withRenderingMode(.alwaysOriginal), forKey: "image")
Run Code Online (Sandbox Code Playgroud)
目标 C
UIAlertAction* action = [UIAlertAction actionWithTitle:@"Action 1" style:UIAlertActionStyleDefault handler:nil];
[action setValue:[[UIImage imageNamed:@"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
Run Code Online (Sandbox Code Playgroud)
干杯:)
| 归档时间: |
|
| 查看次数: |
11187 次 |
| 最近记录: |