[英]Add image on center of Alert ViewController with transparent background
How to display image in center of alert viewController with transparent background of alert viewController. 如何在具有警报viewController透明背景的警报viewController中心显示图像。 I wrote following code.
我写了下面的代码。
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)
It looks like this. 看起来像这样。
Please help me to solve this problem and thanks to all. 请帮助我解决这个问题,并感谢所有人。
you can store you reference of imageView an alertMessage, and in completion of the present viewController you have the exact size of the alert view, so you can adjust the image according to the current alert rect. 您可以将对imageView的引用存储为alertMessage,并且在完成当前viewController时,您将具有警报视图的确切大小,因此可以根据当前警报rect调整图像。
try this code is in swift3 : 试试这个代码在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 doesn't support such changes in view hierarchy you'd be better using a custom extensible component that mimics the behavior of UIAlertController. UIAlertController不支持视图层次结构中的此类更改,最好使用模仿UIAlertController行为的自定义可扩展组件。 The problem with UIAlertController is that you don't know how Apple will change its view hierarchy in the next version of iOS.
UIAlertController的问题在于您不知道Apple将如何在下一版iOS中更改其视图层次结构。 And each time they change something you'll have to add code for a specific version of iOS which is really bad from the maintainability point of view.
而且每次更改时,您都必须为特定版本的iOS添加代码,从可维护性的角度来看,这确实很糟糕。
For example CustomIOSAlertView: https://github.com/wimagguc/ios-custom-alertview . 例如CustomIOSAlertView: https : //github.com/wimagguc/ios-custom-alertview 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.