[英]Add image on center of Alert ViewController with transparent background
如何在具有警报viewController透明背景的警报viewController中心显示图像。 我写了下面的代码。
let image = UIImage(named: "ic_no_data")
let imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40)
let alertMessage = UIAlertController(title: "image", message: "", preferredStyle: .Alert)
let backView = alertMessage.view.subviews.last?.subviews.last
backView?.layer.cornerRadius = 10.0
backView?.backgroundColor = UIColor.lightGrayColor()
alertMessage.view.addSubview(imageView)
let action = UIAlertAction(title:"", style: .Cancel, handler: nil)
action.setValue(image, forKey: "image")
alertMessage .addAction(action)
self.presentViewController(alertMessage, animated: true, completion: nil)
看起来像这样。
请帮助我解决这个问题,并感谢所有人。
您可以将对imageView的引用存储为alertMessage,并且在完成当前viewController时,您将具有警报视图的确切大小,因此可以根据当前警报rect调整图像。
试试这个代码在swift3中:
self.present(alertMessage, animated: true, completion: {
var frame = self.alertMessage.view.frame
frame.origin = CGPoint(x: 0, y: 0) // add your location
self.imageView.frame = frame
})
UIAlertController不支持视图层次结构中的此类更改,最好使用模仿UIAlertController行为的自定义可扩展组件。 UIAlertController的问题在于您不知道Apple将如何在下一版iOS中更改其视图层次结构。 而且每次更改时,您都必须为特定版本的iOS添加代码,从可维护性的角度来看,这确实很糟糕。
例如CustomIOSAlertView: https : //github.com/wimagguc/ios-custom-alertview 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.